BuilderSpec
Programmer
Hi All
I have got some text files which have some 'crap' characters in the middle.. these can be NULL , line feeds etc.just crap in some fields.
I want to be able to process this file and replace the 'crap' characters with an arbitrary character ( eg. '9' )
I have tried reading in the file using fgetc / fputc but cannot determine the real end of file ( the end of file is one such 'crap' character that appears ), Notepad seems to open the file ok with a true end of file. My program stops when it hits the first end of file 'crap' character..
So i changed to using fstat to determoine file size in bytes and then process that many characters but then it never gives a true figure and seems to work for some files but not larger ones....
Any one ever done this ? got any ideas and how to proceed ?
I will post the code I have tried so far...
AnsiString Output = ChangeFileExt(argv[1],".txc");
FILE *fp = fopen(argv[1],"r");
FILE *out = fopen ( Output.c_str() , "w");
char c=0;
struct stat statbuf;
fstat(fileno(fp), &statbuf);
long ct = 0;
while ( ct < statbuf.st_size )
{
c = fgetc(fp);
if ( c != -1 )
{
if ( (isalpha ( c ) && isupper(c)) || isdigit ( c ) || ( c == '\r' || c == '\n' || c == ' ' ) )
fputc(c,out );
else
fputc('9',out);
}
else
if ( ct < (statbuf.st_size -1 ) )
{
c = fgetc(fp);
if ( c != -1 )
fputc ( '9' , out );
else
ungetc( c , fp );
}
ct++;
}
fclose ( out );
fclose ( fp );
return 0;
Any pointers would be greatly appreciated ?
Hope this helps!
Regards
BuilderSpec
I have got some text files which have some 'crap' characters in the middle.. these can be NULL , line feeds etc.just crap in some fields.
I want to be able to process this file and replace the 'crap' characters with an arbitrary character ( eg. '9' )
I have tried reading in the file using fgetc / fputc but cannot determine the real end of file ( the end of file is one such 'crap' character that appears ), Notepad seems to open the file ok with a true end of file. My program stops when it hits the first end of file 'crap' character..
So i changed to using fstat to determoine file size in bytes and then process that many characters but then it never gives a true figure and seems to work for some files but not larger ones....
Any one ever done this ? got any ideas and how to proceed ?
I will post the code I have tried so far...
AnsiString Output = ChangeFileExt(argv[1],".txc");
FILE *fp = fopen(argv[1],"r");
FILE *out = fopen ( Output.c_str() , "w");
char c=0;
struct stat statbuf;
fstat(fileno(fp), &statbuf);
long ct = 0;
while ( ct < statbuf.st_size )
{
c = fgetc(fp);
if ( c != -1 )
{
if ( (isalpha ( c ) && isupper(c)) || isdigit ( c ) || ( c == '\r' || c == '\n' || c == ' ' ) )
fputc(c,out );
else
fputc('9',out);
}
else
if ( ct < (statbuf.st_size -1 ) )
{
c = fgetc(fp);
if ( c != -1 )
fputc ( '9' , out );
else
ungetc( c , fp );
}
ct++;
}
fclose ( out );
fclose ( fp );
return 0;
Any pointers would be greatly appreciated ?
Hope this helps!
Regards
BuilderSpec