By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,128 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,128 IT Pros & Developers. It's quick & easy.

parse strings

P: 1
Hi! I am not a C++ expert, please help getting a little confused.
I'm using Vc++ 6 by the way.

I have this code to read from a text file:

Expand|Select|Wrap|Line Numbers
  1. #include <fstream.h>
  2. char line[100];
  3. void main()
  4.     {  
  5.        ifstream fin("test.txt");
  7.         while( fin.getline(line, 100) ) 
  8.         {
  9.             cout << "read line: " << line << endl;            
  10.         }
  11.     }
my prob is how to parse "line" so that I can store the values that i needed.

For example, if test.txt has the following information,
35 6
I need to store 35 to an int variable, and 6 to another int variable.
or, if I got 344:5
just the same as above, but this time, separate by a colon, not a whitespace..

please help me really lost :(

Thank you very much!
Mar 19 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 2.5K+
P: 3,652
Do you know the delimiter that will be used (a.k.a. do you know there will be a space in between these ints)? If so, you can search for the first occurance of the delimeter (in this case a space) and get the characters before that position. Once you have this, you can perform some simple arithmetic operators to find out what decimal numbers each character represents and form your number.

Alternatively, you could use the >> operator as if you were using cin to get the integer values.
Mar 19 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.