file input by line

This is a discussion on file input by line within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to read a data (*.txt) file of records separated by commas (,). I would like to ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    file input by line

    Hello,

    I am trying to read a data (*.txt) file of records separated by commas (,).

    I would like to read each line up to the comma and separate the fields into variables for comarison.

    Here is a sample line:
    Code:
    BUTLER,MARION TOWNSHIP,C:\pec\complete\raster\BUTLER\barkeyville_pa_se\barkeyville_pa_se.tfw
    Here is my code to process the line:

    Code:
    	//open the imageFile again with the character path argument
    	ifstream municIn;
    	municIn.open(imageFileName);
    
    	char c;							//current character
    
    	char line[400] = "";			//buffer for each line
    	char * linePtr = &line[0];		//point the pointer to the beginning of line
    	
    	char field[150] = "";			//buffer for each field
    	char * fieldPtr = &field[0];	//point to the beginning of field
    
    	//read in the line
    	while(municIn.getline(line, 400, '\n'))
    	{	
    		
    		//read characters up to ','
    		while( (c = municIn.get()) != ',' )
    		{
    			//add the character to the field
    			*fieldPtr = c;
    			fieldPtr++;
    		}		
    
    		//cap off the field
    		fieldPtr = '\0';
    		fieldPtr = &field[0];
    	
    		cout << field << endl;
    		
    	}	//end while line in
    
    	getch();
    
    	municIn.close();
    The problem is Windows continuosly issues an error message and says the program must be closed.

    What am I doing wrong?
    Last edited by VanJay011379; 08-01-2002 at 04:29 PM.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    	//read in the line
    	while(municIn.getline(line, 400, '\n'))
    	{
    		//read characters up to ','
    		linePtr = line;
    		while( *linePtr != ',' )
    		{
    			//add the character to the field
    			*fieldPtr = *linePtr++;
    			fieldPtr++;
    		}		
    
    		//cap off the field
    		fieldPtr = '\0';
    		fieldPtr = &field[0];
    	
    		cout << field << endl;
    		
    	}	//end while line in

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Thanks.

    However, the line:
    Code:
    fieldPtr = '\0';
    needs to be:
    Code:
    *fieldPtr = '\0';
    And it works! Thanks again.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21