Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C++ (noob) what's wrong with my code?
New Posts  All Forums:Forum Nav:

C++ (noob) what's wrong with my code?

post #1 of 2
Thread Starter 
The idea was to have with work on ANY file giving me the total number of in hex characters. It seems to be more or less accurate on a text file but other file types are giving numbers that are WAY too low to be realistic. I ran this against a ~1GB mp4 that gave me 12K back. what am i missing?
Code:
#include <fstream>
#include <iostream>
using namespace std ;

int main()
{
    char letter ;
    int i ;
    cout << "Input the filename:" << endl;
    string stringinput;
        cin >> stringinput;
    ifstream file( stringinput.c_str() ) ;
    if( ! file )
    {
        cout << "Error opening input file, " << ( stringinput ) << ". Check file path and try again." << endl ;
        return -1 ;
    }
    else
        for( i = 0; ! file.eof() ; i++ )
        {
            file.get( letter ) ;
            //cout << hex << (int) letter;
        }
        cout << endl;
        float k = 1024, m = 1048576;
        file.close();
        //cout << a << endl;
        if( i < 1024)
        {
            cout << "Total: " << dec << i << endl;
        }
        else if( i < m)
        {
            cout << "Total: " << dec << (i / k) << "K" << endl;
        }
        else
        {
            cout << "Total: " << dec << (i / m) << "M" << endl;
        }
        return 0 ;
}
dBag
(9 items)
 
yep...
(10 items)
 
air
(8 items)
 
CPUMotherboardRAMHard Drive
xeon e3-1230v2 supermicro mbd x9scm-0 KVR1333D3E9S/4G OCZ vertex3 60GB 
CoolingOSPowerCase
1u 80mm blower + passive cpu  proxmox 3.1 supermicro 300w 1u supermicro 1u 
Other
2x intel pro1000 nics  
CPUMotherboardGraphicsRAM
A8-3870K @ 3.7GHz GIGABYTE A75M-UD2H Power Cooler 7870 Team Group Inc 
Hard DriveCoolingOSPower
3x OCZ Agility 3 60GB (RAID-0 on Rocket RAID 27... Corsair H-80 core win-7 Seagate 750 Gold 
CaseAudio
some lian-li case... M-Audio AV40s + 10" Sony ___? 
CPUMotherboardGraphicsRAM
D525 @ 1.8GHz Supermicro onboard 2x2GB DDR1333 
Hard DriveOSCaseOther
WD Green ubuntu 12 1u  3x40mm fans  
  hide details  
Reply
dBag
(9 items)
 
yep...
(10 items)
 
air
(8 items)
 
CPUMotherboardRAMHard Drive
xeon e3-1230v2 supermicro mbd x9scm-0 KVR1333D3E9S/4G OCZ vertex3 60GB 
CoolingOSPowerCase
1u 80mm blower + passive cpu  proxmox 3.1 supermicro 300w 1u supermicro 1u 
Other
2x intel pro1000 nics  
CPUMotherboardGraphicsRAM
A8-3870K @ 3.7GHz GIGABYTE A75M-UD2H Power Cooler 7870 Team Group Inc 
Hard DriveCoolingOSPower
3x OCZ Agility 3 60GB (RAID-0 on Rocket RAID 27... Corsair H-80 core win-7 Seagate 750 Gold 
CaseAudio
some lian-li case... M-Audio AV40s + 10" Sony ___? 
CPUMotherboardGraphicsRAM
D525 @ 1.8GHz Supermicro onboard 2x2GB DDR1333 
Hard DriveOSCaseOther
WD Green ubuntu 12 1u  3x40mm fans  
  hide details  
Reply
post #2 of 2
If I remember correctly, cin will stop at the first whitespace it finds. If your file has a whitespace in between words it will not work. For string's I've always used getline (cin, "myVariable");

I may simply not understand what it is your trying to accomplish. Let me know if this helps.

Sent from my Transformer TF101 using Tapatalk 2
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › C++ (noob) what's wrong with my code?