# C++, Can I read an int from a .txt file? SOLVED!

## Recommended Posts

darenking    122
Howdy. I'm creating a routine to read text files and perform graphic loading operations based on the findings. An example text file looks like this:
createspritesload
data/001_sprites_smally.bmp

createspritescopyfliphorz
1
6


The first command, 'createspritesload', instructs the program to load in the bitmap file named in the next line (the program then creates sprites from this file). The second command, 'createspritescopyfliphorz', is supposed to instruct the program to create copies of some of these sprites and flip them horizontally. It all works fine apart from, I need the program to read in the two values (1 and 6 in this case) after 'createspritescopyfliphorz'. These values tell the program to copy and flip six sprites starting from sprite number 1. I know how to read text from the file and store the text in a string:
	std::string line;
while(file)
{
getline(file, line);
//etc
}


But how can I read a value and store it in an integer? I know how to read ints from binary files but of course this is a text file. Or do I load the line with getline and convert the contents to an int? If so, how? Thanks! _ [Edited by - darenking on July 5, 2005 2:03:21 AM]

##### Share on other sites
parse it and convert all the ints you find and want to convert with atoi, in cstdlib.

##### Share on other sites
Drew_Benton    1861
Working with text files is a lot simpler than binary files. If you have something like:

MyFile.txt
String Here
1
6

You can simply do this:
ifstream if("file.txt");if.getline(); // MyFile.txtif.getline(); // String Hereint val1;if >> val1; // stores 1int val2;if >> val2; // stores 6

That's it! Working with text files is just like using cin/cout, except you are using the fin/fout features of the file stream. Take a look at File I/O

##### Share on other sites
snk_kid    1312
Quote:
 Original post by darenkingBut how can I read a value and store it in an integer? I know how to read ints from binary files but of course this is a text file.

C++ I/O streams preform formatting/parsing to and from types when doing formatted I/O so there isn't anything special to be done, use the stream insertion/extraction operators e.g.

int foo = 0, bar = 0, foobar = 0;in_file_stream >> foo >> bar >> foobar;

Quote:
 Original post by darenkingOr do I load the line with getline and convert the contents to an int? If so, how?

Well there isn't a need to do this but if you want to parse a string into several types you can use string streams (think of a C++ file stream but for C++ strings).

##### Share on other sites
darenking    122
Quote:
 Original post by snk_kidint foo = 0, bar = 0, foobar = 0;in_file_stream >> foo >> bar >> foobar;

This is ace. Can you use it to load text or do you have to use getline? I've tried it with text and it doesn't seem to work.

##### Share on other sites
Quote:
Original post by darenking
Quote:
 Original post by snk_kidint foo = 0, bar = 0, foobar = 0;in_file_stream >> foo >> bar >> foobar;

This is ace. Can you use it to load text or do you have to use getline? I've tried it with text and it doesn't seem to work.

If I remember correctly, if you >> with strings, it'll read the string up until it hits whitespace. So each >> will read another word. You'll use getline when you want the entire line, as the name implies.

##### Share on other sites
darenking    122
Yes, I've just stuck it in my program and that does seem to be how it works. I can now read in this:

...and this...

createspritescopyfliphorz 1 6

and my program will load in the sprites.bmp file and create sprites for all the images it contains, then will copy and flip 6 of these sprites, starting with sprite number 1.

Problem solved!

Thanks all.

##### Share on other sites
Zahlman    1682
Of course, you can always getline() a line at a time, and then parse each line when necessary by wrapping a stringstream around it and then extracting from *that* (in the same way as you would from the file stream). :)