![]() |
| | #1 |
| Registered User Join Date: Jun 2002
Posts: 59
| File input output problem 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
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
__________________ " . . . and I lay awake, big dreamers never sleep." - David Lee Roth |
| VanJay011379 is offline | |
| | #2 |
| Guest
Posts: n/a
| 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 |
| Guest
Posts: n/a
| 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. |
|
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |
| Outputting to a File Modified Text | alpha | C++ Programming | 8 | 11-24-2003 08:39 PM |
| Confusing problem with file input | Vorok | C++ Programming | 3 | 01-05-2003 03:49 AM |
| problem with output | Garfield | C Programming | 2 | 11-18-2001 08:34 PM |
| Simple File Creation Algorithm | muffin | C Programming | 13 | 08-24-2001 03:28 PM |