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();
converting from char to int using filestream::getline
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.
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?
There is a newer version of getline that will read directly into a string.
Alternatively, you could read directly into an integer:
Finally, if you have a string and want an int from it, you can use stringstreams:
[edited by - sjelkjd on April 18, 2003 3:32:48 PM]
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]
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?
You shouldn''t have to do anything differently. Make sure the file is accessible from your new project.
Also bear in mind that .eof() is only valid after an operation. That means that your first getline() could set eof() to true, but you carry on reading from it anyway (which is bad/wrong).
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement