Thread: parse

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    72

    parse

    hello everyone, i have a problem. Im reading a .txt file in like this:

    Code:
    Doe,John           20
    it fills the array: list[10]

    I would like to put 20 into an integer array, and Doe, John into a string array. can someone give me a hand on the code. Thanks really appreciate it.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You could use scanf or you could make code that reads text up to the first comma (the last name) then reads the string up to the first white-space (first name) reads data after the first white-space and if it is a letter then it must be a middle initial and if it is a number then it must be whatever that number you have in your example indicates.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    how do i make it read up to the first space?

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Read in a whole line using getline and then parse the string by doing something like:
    Code:
    string strLine;
    getline(inFile, strLine);
    int comma = strLine.find_first_of(",");
    ....

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    this is what I have so far.

    Code:
    #include <cstring.h>
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    
    main()
    {
          string strLine;
       	fstream inFile;
    		inFile.open("test.txt",ios::in);
    		int size;
    		size = 0;
    		while( !inFile.eof() )
    		{
      			getline(inFile, strLine);
      		}
    		inFile.close();
    
    
    	int space = strLine.find_first_of(" ");
       
    
    
    
    
    }
    don't know what to do now though

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    72
    Once you know where the first space char is, you can copy all the chars prior to that into your string array, and the converted int to your int array
    i tried this, but it gives me errors.

    Code:
    #include <cstring.h>
    #include <iostream.h>
    #include <conio.h>
    #include <fstream.h>
    #include <stdio.h>
    
    int main()
    {
        string strLine;
        fstream inFile;
    
        inFile.open( "marks.txt",ios::in );
        int size;
        size = 0;
        while( getline( inFile, strLine ) )
        {
            //getline( inFile, strLine );
            cout << strLine << endl;
            int space = strLine.find_first_of( " " );
            cout << "First space at position " << space << endl;
            int res = atoi(&(strLine.c_str())[space]);
            cout << "Converted int is " << res << endl;
            getch();
    
        }
            inFile.close( );
        char *p;
    
    
        p = strtok(strLine, " ");
        cout << p << endl;
    
        getch();
    }
    Could i get some help with it.

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Why make things so complicated?

    Code:
    #include<iostream.h>
    #define MAX_LINES 20
    #define MAX_NAME_LEN 50
    int main()
    {
        char names[MAX_NAME_LEN][MAX_LINES];
        char buff[64];
        int nums[MAX_LINES];
        Ifstream inFile;
        int i;
    
        inFile.open( "marks.txt" );
        if( !inFile.is_open() ) return (1);
    
        for(i=0; !inFile.eof(); ++i)
        {
            cin >> names[i]; // will read up until the whitespace
            cout "Line# " << i << " name: " << names[i];
            cin >> nums[i];
            cout << " number: " << nums[i] << endl; 
            cin.ignore(1);
            cin.get();
    
        }
        inFile.close( );
        cin.get();
        return (0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  2. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM