Jump to content
  • Advertisement

Archived

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

wizard341

converting from char to int using filestream::getline

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!