Archived

This topic is now archived and is closed to further replies.

converting from char to int using filestream::getline

This topic is 5542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Im reading in a file with filestream and trying to parse some of the data. I can read in characters fine, but im having trouble getting some of the data ive read from character to int.

ifstream FileIn;
char buffer[256];
char string1[] = "test";
char string2[] = "test2";
int var1;
int var2;

FileIn.open("inFile.txt");
while(FileIn.eof() ==false)
{
FileIn.getline(buffer,255,'' '');

cout<<buffer;
if(strcmp(string1,buffer) == 0)
{
cout<<" "<<FileIn.getline(buffer,255,'' '');
cout<<" "<<FileIn.getline(buffer,255,'' '')<<endl;
}

if(strcmp(string2,buffer) == 0)
{
FileIn.getline(buffer,255,'' '');
cout<<" "<<buffer<<endl;
}

}

FileIn.close();

Im trying to read in a word, and then 2 numbers after it, but i cant seem to get it to (for lack of better word, typecast) from the char[] to an int. Ive done this before but i cant remember what i did. Anyone have a guess on what to do?

Share on other sites
atoi would probably work.. really depends on the size of your numbers

Share on other sites
supposing the numbers < 4000? would it still work?

Share on other sites
There is a newer version of getline that will read directly into a string.

      ifstream in;string line;getline(in,line,' '); //reads up until the first space

Alternatively, you could read directly into an integer:

  ifstream in;string s;int i;in>>s>>i; //gets a string, then the int after it

Finally, if you have a string and want an int from it, you can use stringstreams:

  string s;...istringstream in(s);int n;in>>n; //parse an int out of s

[edited by - sjelkjd on April 18, 2003 3:32:48 PM]

Share on other sites
I got it to work in a standard console application, but im having trouble tyring it get it work in a win 32 app. I had assumed i could use streams, but then i remember windows dosnt like them too much. Anyone know of a way i can do this using win32 api''s?

Share on other sites
You shouldn''t have to do anything differently. Make sure the file is accessible from your new project.

• 18
• 11
• 16
• 9
• 51
• Forum Statistics

• Total Topics
631396
• Total Posts
2999786
×