parse strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • proclivity
    New Member
    • Mar 2007
    • 1

    parse strings

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

    I have this code to read from a text file:

    Code:
    #include <fstream.h>
    char line[100];
    void main()
        {  
    	   ifstream fin("test.txt");
    
    		while( fin.getline(line, 100) ) 
    		{
    			cout << "read line: " << line << endl;			
    		}
        }
    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 out..im really lost :(

    Thank you very much!
    Last edited by Ganon11; Mar 19 '07, 03:05 PM. Reason: code tags added
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    Working...