Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing CR/LF from a file

Status
Not open for further replies.

dnfrantum

Programmer
Oct 23, 2001
175
0
0
US
I know how to insert CR/LF in a document, but how would I remove them. I have an flat file that I want to remove each CR/LF to create one long string. Can someone steer me in the right direction.



Thanks in advance,
Donald
 
To use an existing file you could use any line reading mechanism (there are several) which returns only the text of the line thereby stripping the CRLF bytes from the input stream. Then all you have to do is write the data in memory to a file.

-pete
 
Something like this will do it - you'll have to add your own error checking as required.....


FILE * fptr_in;
FILE * fptr_out;
char sz_buffer[1024];
char * ptr;

if((fptr_in=fopen("test.c","r"))==NULL)
return 1;
if((fptr_out=fopen("output","w"))==NULL)
{
fclose(fptr_in);
return 1;
}
while(fgets(sz_buffer,1023,fptr_in)!=0)
{
if((ptr=strrchr(sz_buffer,'\n'))!=NULL)
*ptr=0x00;
fwrite(sz_buffer,strlen(sz_buffer),1,fptr_out);
}
fclose(fptr_in);
fclose(fptr_out);
return 0;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top