String to int conversion

May 26, 2012 at 7:15pm
The following code is supposed to read in a line from a.txt file and identify two integers separated by a whitespace character. It doesn't seem to work however and I really don't understand why. Any help would be fantastic!

fstream out_file;
out_file.open("tracetest1.txt", ios::out);

for (int i=0;i<10;i++)
{
out_file<<hex<<i+1000<<" "<<i+1000000<<endl;
}

out_file.close();

int CPU[2][10];


fstream input_file;
input_file.open("tracetest1.txt", ios::in);
if (!input_file.good())
{
cout << "FATAL ERROR: Could not open file 'trace.dat'";
system("PAUSE");
exit(1);
}

for (int i = 0; i < 10; i++)
{
input_file >> CPU[0][i];
input_file >> CPU[1][i];
cout<< hex<< CPU[0][i] << " " <<CPU[1][i]<< endl;
}

input_file.close();


please share any insight you can
May 26, 2012 at 8:04pm
When you say it doesn't work, what do you mean? What are you expecting it to output?

-Albatross
May 26, 2012 at 10:12pm
It outputs a bunch of c's:

ccccccccccc ccccccccccccc
cccccccccc cccccccccccccc

etc.

it is supposed to output the numbers it's read in from the file it creates, but it doesn't.
May 26, 2012 at 11:34pm
You're missing some hexes in your program. If your program doesn't know in advance that what it's reading in is a hexadecimal number, then how's it supposed to know that it's not garbage? :)

-Albatross
Last edited on May 26, 2012 at 11:35pm
Topic archived. No new replies allowed.