C++/text file
Expert: Prince M. Premnath - 9/14/2006
QuestionDear Sir,
Thank you very much for your helping .
I am trying to write a code that can read a specific range from text file .However , I found this code which is 100% fine and can do the following , changing the following input file to the following output file .
The input file is
1 AA
2 BB
3 CC
4 DD
5 EE
6 FF
The output file is
1
2
3
4
5
6
And it deals just with this format. So I will give you the whole code ( As I said it does not have any problem )and it will be helpful if you can add explanations for every command ,what it does and how can be improved to match my need.
In general , I want to improve this code to do the following , changing the the input file to the output file
The input file is
# Time Delta Pg Vt Vr Freq
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
The output file is
Delta
2
2
2
2
2
Thank you very much
The code is
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
int clean_up( FILE* pfIn, FILE* pfOut, char* pData )
{
int rc = 0;
if( pfIn != NULL )
{
rc = fclose( pfIn );
pfIn = NULL;
}
if( pfOut != NULL )
{
rc = fclose( pfOut );
pfOut = NULL;
}
if( pData != NULL )
{
free( pData );
pData = NULL;
}
return rc;
}
int main( void )
{
int rc = 0;
int last_error = 0;
FILE* pfInput = 0;
FILE* pfOutput = 0;
char szInFileName[256] = {0};
char szOutFileName[256] = {0};
char* pData = 0;
char* separators = "\n\t ";
char* token;
struct stat file_info = {0};
printf( "Enter the name of the file to read and process: " );
fgets( szInFileName, sizeof( szInFileName ), stdin );
printf( "Enter the name of the file for processed data: " );
fgets( szOutFileName, sizeof( szOutFileName ), stdin );
// strip off the trailing \n that fgets give us
szInFileName[-1 + strlen( szInFileName )] = 0;
szOutFileName[-1 + strlen( szOutFileName )] = 0;
rc = stat( szInFileName, &file_info );
if( rc )
{
last_error = errno;
printf( "File: %s\nError Number: %d\nError: %s\n", szInFileName, last_error, strerror( last_error ) );
exit( last_error );
}
else // open file
{
pfInput = fopen( szInFileName, "r" );
if( !pfInput )
{
last_error = errno;
printf( "File: %s\nError Number: %d\nError: %s\n", szInFileName, last_error, strerror( last_error ) );
clean_up( pfInput, pfOutput, pData ); // don't need this...
exit( last_error );
}
else // read file contents into buffer
{
pData = (char*)calloc( 1 + file_info.st_size, sizeof( char ) );
if( !pData )
{
last_error = errno;
printf( "Memory Allocation: %d bytes\nError Number: %d\nError: %s\n", (int)(1+ file_info.st_size), last_error, strerror( last_error ) );
clean_up( pfInput, pfOutput, pData );
exit( last_error );
}
else
{
rc = fread( pData, sizeof( char ), file_info.st_size, pfInput );
if( rc != file_info.st_size && errno != 0 )
{
last_error = errno;
printf( "File: %s\nRead: %d bytes\nError Number: %d\nError: %s", szInFileName, (int)(1+ file_info.st_size), last_error, strerror( last_error ) );
clean_up( pfInput, pfOutput, pData );
exit( last_error );
}
else // open the output file
{
pfOutput = fopen( szOutFileName, "w" );
if( !pfOutput )
{
last_error = errno;
printf( "File: %s\nError Number: %d\nError: %s", szOutFileName, last_error, strerror( last_error ) );
clean_up( pfInput, pfOutput, pData );
exit( last_error );
}
else // parse the data
{
token = strtok( pData, separators );
while( token != NULL )
{
if( atoi( token ) != 0 ) // write the IDs to the file
{
rc = fwrite( token, strlen( token ), 1, pfOutput );
last_error = errno;
if( rc < (int)strlen( token ) && last_error != 0 )
{
printf( "File: %s\nError Number: %d\nError: %s\nToken: %s\n", szOutFileName, last_error, strerror( last_error ), token );
clean_up( pfInput, pfOutput, pData );
exit( last_error );
}
rc = fwrite( "\n", 1, 1, pfOutput );
}
token = strtok( NULL, separators );
}
}
}
}
}
}
rc = clean_up( pfInput, pfOutput, pData );
return rc;
}
AnswerOk The problem is so simple !
The code you have written is too obscure for the given task
yet you can make simpler !
1. Insted of writing indivdual data's write as a record in the file , so it will be easy for you to retrive the required information in a required format
2.First create the input file using the record structure.
3.OPen the output file.
4.Read the input file sequentailly and write only the required data to the output file.
For any more help aks a follow up!
START NOW!
THANK YOU !