File input output problem

This is a discussion on File input output problem within the C++ Programming forums, part of the General Programming Boards category; Hello, I am simply trying to read a file and output particular contents to another file. I am reading a ...

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

    File input output problem

    Hello,

    I am simply trying to read a file and output particular contents to another file.

    I am reading a file of comma delimited fields and evaluating each field to determine if the line (a record) should be written to another file.

    For some reason, my code refuses to output the field once it is read in. I inserted some debugging statements (cout) and it appears the fields are read in correctly but cannot be output.

    I have trimmed my code back to the bare problem:

    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <string.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main()
    {
    	ifstream municIn;
    	municIn.open("C:\\temp\\test\\municImg.txt");
    
    	ofstream municOut;
    	municOut.open("C:\\temp\\test\\municImgOut.txt");
    
    	if(!municIn)
    	{
    		cerr << "Could not open input file" << endl;
    		exit(1);
    	}
    
    	//hold lines of text and point to current character
    	char line[500] = "";
    	char * linePtr = &line[0];
    
    	//hold fields and point to current character
    	char county[150] = "";
    	char * countyPtr = &county[0];
    
    	//hold fields and point to current character
    	char munic[150] = "";
    	char * municPtr = &munic[0];
    
    	//hold fields and point to current character
    	char location[250] = "";
    	char * locationPtr = &location[0];
    
    	//hold fields and point to current character
    	char locCounty[150] = "";
    	char * locCountyPtr = &locCounty[0];
    
    	while(municIn.getline(line, 550, '\n'))
    	{
    		//add the current character to the field buffer
    		while(*linePtr != ',')
    		{ *countyPtr++ = *linePtr++; }
    
    		//after the field is complete (a ',' encountered)
    		//cap it off and return the pointer to the first character of the field
    		*countyPtr = '\0'; countyPtr = &county[0];
    		cout << county << ",";
    
    		++linePtr;	//increment to the next char after the last delimiting ','
    
    		//read for the munic
    		while(*linePtr != ',')
    		{ *municPtr++ = *linePtr++; }
    		
    		//cap off and point to beginning of munic
    		*municPtr = '\0'; municPtr = &munic[0];
    		cout << munic << ",";
    	
    		++linePtr;	//increment to the next char after the last delimiting ','
    
    		//read the munic up to the next ','
    		//add the current character to the field buffer
    		while(*linePtr != ' ')
    		{ *locationPtr++ = *linePtr++; }
    		
    		//cap off and point to beginning of location
    		*locationPtr = '\0'; locationPtr = &location[0];
    		cout << location << ",";
    
    	}	//end while
    
    	municIn.close();
    	municOut.close();
    
    	return 0;
    
    }	//end main
    Here is some sample input:

    Code:
    CO_NAME,MCD_NAME,image_location
    BUTLER,MARION TOWNSHIP,S:\data\raster\BUTLER\barkeyville_pa_se\barkeyville_pa_se.tif
    BUTLER,MARION TOWNSHIP,S:\data\raster\BUTLER\barkeyville_pa_sw\barkeyville_pa_sw.tif
    BUTLER,MARION TOWNSHIP,S:\data\raster\BUTLER\west_sunbury_pa_ne\west_sunbury_pa_ne.tif
    BUTLER,MARION TOWNSHIP,S:\data\raster\BUTLER\west_sunbury_pa_nw\west_sunbury_pa_nw.tif
    BUTLER,VENANGO TOWNSHIP,S:\data\raster\BUTLER\barkeyville_pa_se\barkeyville_pa_se.tif
    What is wrong with my code? Why won't the cout statement output the fields?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    elad
    Guest
    I don't follow. How do you know the fields are being read in properly if you aren't able to generate appropriate ouptut?

    I do have a couple thoughts. If you are able to ouptput the county and munic fields but not the location field, it may be due to the backslashes in the location fields. You may need to store double backslashes in the location field so that you can output a single backslash since the first backslash may be read by the output stream as a "keyword" or escape character or whatever the technical term is for the initial backslash character in C++.

    Second, I would increase the size of line to 550 in its' declaration, although that might well be a typo.

    Third, the while loop used to terminate the location field read should look for the NULL character terminating the line buffer, not look for a space, unless you know for sure there is a space after the last visible char in the location field.

  3. #3
    elad
    Guest
    if you are able to oupt all three fields to the screen with the cout statements, then I suggest you add code to actually use municOut, as other than declaring and closing that stream, it is never used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Outputting to a File Modified Text
    By alpha in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2003, 07:39 PM
  3. Confusing problem with file input
    By Vorok in forum C++ Programming
    Replies: 3
    Last Post: 01-05-2003, 02:49 AM
  4. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 07:34 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

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