Home › Forums › C Programming › Reading first column in a file and creating new files…
- This topic has 1 reply, 2 voices, and was last updated 15 years, 4 months ago by GWILouisaxwzkla.
Viewing 1 reply thread
- AuthorPosts
- August 10, 2009 at 11:42 am #2220GarnetHuynhParticipant
Hi,
Could you please assist me with the ‘C’ code for the following :-
I have a file called Test.txt.
I) Test.txt
A,BC,DE,HI,00,11
A,DE,HI,JK,23,24
A,XX,YY,YU,40,95
B,CD,EF,GH,11,22
B,JJ,KK,JU,45,89
C,DE,FG,HI,22,33
C,EF,GH,IJ,77,88
D,EF,GH,IJ,33,44
D,FG,HI,JK,44,55Need to read the first column of the file and create files such that for each change in the first column, a new file is created with the filename as first column.ref.
That is for above file, 4 Files are created as each time the first column changes, a new file is created. The 4 files generated are as follows:-
II)
1) A.ref
BC,DE,HI,00,11
DE,HI,JK,23,24
XX,YY,YU,40,952) B.ref
CD,EF,GH,11,22
JJ,KK,JU,45,893) C.ref
DE,FG,HI,22,33
EF,GH,IJ,77,884) D.ref
EF,GH,IJ,33,44
FG,HI,JK,44,55Best Regards,
Marconi. - August 11, 2009 at 6:00 pm #3601GWILouisaxwzklaParticipant
This seems to work:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : August,10,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 3:17:50 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br /><br />using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />//CHANGE FILE NAMES TO WHAT YOU WANT HERE !!!!!!!!!!!!!!!!!!!!!<br /><br />FILE * inputFile = fopen ( "c:\programs\data.txt", "r+" );<br /><br />FILE * outputFile1 = fopen ( "c:\programs\data2.txt", "w+" );<br />FILE * outputFile2 = fopen ( "c:\programs\data3.txt", "w+" );<br />FILE * outputFile3 = fopen ( "c:\programs\data4.txt", "w+" );<br />FILE * outputFile4 = fopen ( "c:\programs\data5.txt", "w+" );<br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />char ch;<br />char newline = 10;<br /><br />ch = fgetc ( inputFile );<br />fgetc ( inputFile );<br />while ( ! feof ( inputFile ) )<br />{<br /><br />if ( ch == 'A' )<br />{<br />ch = fgetc ( inputFile );<br />while ( ch != newline )<br />{<br />fputc ( ch , outputFile1 );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( 'n' , outputFile1);<br />}<br />if ( ch == 'B' )<br />{<br />ch = fgetc ( inputFile );<br />while ( ch != newline )<br />{<br />fputc ( ch , outputFile2 );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( 'n' , outputFile2 );<br />}<br />if ( ch == 'C' )<br />{<br />ch = fgetc ( inputFile );<br />while ( ch != newline )<br />{<br />fputc ( ch , outputFile3 );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( 'n' , outputFile3);<br />}<br />if ( ch == 'D' )<br />{<br />ch = fgetc ( inputFile );<br />while ( ch != newline )<br />{<br />fputc ( ch , outputFile4 );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( 'n' , outputFile4);<br />}<br /><br />ch = fgetc ( inputFile );<br />fgetc ( inputFile );<br />}<br />fclose ( outputFile1 );<br />fclose ( outputFile2 );<br />fclose ( outputFile3 );<br />fclose ( outputFile4 );<br /><br />return 0 ;<br />}<br /><br /><br /><br /><br />
- AuthorPosts
Viewing 1 reply thread
- The forum ‘C Programming’ is closed to new topics and replies.