Home Forums C Programming Reading first column in a file and creating new files… Re: Re: Reading first column in a file and creating new files…

#3601
GWILouisaxwzkla
Participant

This seems to work:


/****************************************************************
* File Name : c:programstempCG.cpp
* Date : August,10,2009
* Comments : new project
* Compiler/Assembler : Visual C++ 6.0
* Modifications :
*
*
*
*
*
* Program Shell Generated At: 3:17:50 p.m.
=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


#include
#include
//#include
//#include
//#include
//#include

using namespace std;

//main function ******************************

int main ( )
{

//CHANGE FILE NAMES TO WHAT YOU WANT HERE !!!!!!!!!!!!!!!!!!!!!

FILE * inputFile = fopen ( "c:\programs\data.txt", "r+" );

FILE * outputFile1 = fopen ( "c:\programs\data2.txt", "w+" );
FILE * outputFile2 = fopen ( "c:\programs\data3.txt", "w+" );
FILE * outputFile3 = fopen ( "c:\programs\data4.txt", "w+" );
FILE * outputFile4 = fopen ( "c:\programs\data5.txt", "w+" );

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

char ch;
char newline = 10;

ch = fgetc ( inputFile );
fgetc ( inputFile );
while ( ! feof ( inputFile ) )
{

if ( ch == 'A' )
{
ch = fgetc ( inputFile );
while ( ch != newline )
{
fputc ( ch , outputFile1 );
ch = fgetc ( inputFile );
}
fputc ( 'n' , outputFile1);
}
if ( ch == 'B' )
{
ch = fgetc ( inputFile );
while ( ch != newline )
{
fputc ( ch , outputFile2 );
ch = fgetc ( inputFile );
}
fputc ( 'n' , outputFile2 );
}
if ( ch == 'C' )
{
ch = fgetc ( inputFile );
while ( ch != newline )
{
fputc ( ch , outputFile3 );
ch = fgetc ( inputFile );
}
fputc ( 'n' , outputFile3);
}
if ( ch == 'D' )
{
ch = fgetc ( inputFile );
while ( ch != newline )
{
fputc ( ch , outputFile4 );
ch = fgetc ( inputFile );
}
fputc ( 'n' , outputFile4);
}

ch = fgetc ( inputFile );
fgetc ( inputFile );
}
fclose ( outputFile1 );
fclose ( outputFile2 );
fclose ( outputFile3 );
fclose ( outputFile4 );

return 0 ;
}