HOW-TO Truncate first line in a file to zero length?

A

Amit Kulkarni

Hi,
I have small problem.
I want to truncate a line in a text file using C file handling
functions and write new line in place of it. How do I do it?

e.g.
"example.txt"
Line 1: This is a text file.
Line 2: Second line of it.

Now I want to truncate first line and replace it with suppose
following text: "This is first line"

How do I do it using C file handling functions like fputs, fscanf,
fseek.

Thnks in advance

------------------------------------------------------------------------------------------------------------------------------------------
Amit Kulkarni.
--------------------------------------------------------------------------------------------------------------------------------------------|
Everyone wishes to have truth on his side, but not everyone wishes to
be on the side of truth.
--------------------------------------------------------------------------------------------------------------------------------------------|
 
D

dbtid

Amit said:
Hi,
I have small problem.
I want to truncate a line in a text file using C file handling
functions and write new line in place of it. How do I do it?

e.g.
"example.txt"
Line 1: This is a text file.
Line 2: Second line of it.

Now I want to truncate first line and replace it with suppose
following text: "This is first line"

How do I do it using C file handling functions like fputs, fscanf,
fseek.

Thnks in advance

------------------------------------------------------------------------------------------------------------------------------------------
Amit Kulkarni.
--------------------------------------------------------------------------------------------------------------------------------------------|
Everyone wishes to have truth on his side, but not everyone wishes to
be on the side of truth.
--------------------------------------------------------------------------------------------------------------------------------------------|

It's not hard.

a) Read the whole file in to a bunch of character buffers that are
appropriately sized.
b) Change the first one.
c) Write all the buffers back out with something like fprintf or fputs.

Stream text files like this don't really support the ability to just
replace things in other ways because they aren't record-based. On some
OS's this was possible because even text files were treated like they
were records of (len, text), (len, text), etc.

But for the most part, Unix and other operating systems treat text files
as streams of bytes. So the only way to shorten or lengthen the stream
is to read all of the data in, replace just the part you want, and then
write out the whole thing all over again.

<OT non-ANSI C>
Now, if you want to do something that's a little simpler to code,
consider doing something like invoking sed on it and tell IT to
change line 1 to something that you want. That's an exercise I'll
leave to you :)
</OT>

HTH
dbtid
 
I

Irrwahn Grausewitz

a) Read the whole file in to a bunch of character buffers that are
appropriately sized.
b) Change the first one.
c) Write all the buffers back out with something like fprintf or fputs.

It's unnecessary and extremely wasteful WRT memory usage to buffer
the whole file contents. Writing the new first line to a temporary
file and streaming the remainder of the original file seems much more
appropriate, especially for huge files.

<snip>

Regards
 
A

Al Bowers

Amit said:
Hi,
I have small problem.
I want to truncate a line in a text file using C file handling
functions and write new line in place of it. How do I do it?

e.g.
"example.txt"
Line 1: This is a text file.
Line 2: Second line of it.

Now I want to truncate first line and replace it with suppose
following text: "This is first line"

How do I do it using C file handling functions like fputs, fscanf,
fseek.

FAQ Question 19.14
How can I insert or delete a line (or record) in the middle of a file?
located at: http://www.eskimo.com/~scs/C-faq/q19.14.html
seems to be related to your question.

The faq says that you probably can't, short of rewriting the file.
To rewrite the file, I believe the best approach is as you read the
input file, example.txt, copy it to a temp file, replacing the
line to delete with the replacement line. Then rename the temp file
to example.txt.

Function replaceline defined below is an untested example:

#include <stdio.h>

int replaceline(const char *fname, unsigned linenr,
const char *replacetxt);

int main(void)
{
if(replaceline("example.txt",1,"This is the first line"))
puts("The file was successfully modified");
else puts("The file was not sucessfully modified");
return 0;
}

int replaceline(const char *fname,unsigned linenr,
const char *replacetxt)
{
FILE *fpi,*fpo;
int ch;
unsigned count;

if(!fname || !linenr || !replacetxt) return 0;
if((fpi = fopen(fname,"r")) == NULL)
{
perror("Error opening file for reading");
return 0;
}
if((fpo = fopen("temp.txt","w")) == NULL)
{
perror("Error opening file time.txt");
fclose(fpi);
return 0;
}
for(count = 1; (ch = fgetc(fpi)) != EOF; )
{
if(count == linenr)
{
while((ch = fgetc(fpi)) != EOF && ch != '\n') ;
if(0 > fprintf(fpo,"%s\n",replacetxt))
{
fclose(fpi);
fclose(fpo);
return 0;
}
count++;
if(ch == EOF) break;
}
else if(EOF == fputc(ch,fpo))
{
fclose(fpi);
fclose(fpo);
return 0;
}
if(ch == '\n') count++;
}
fclose(fpo);
if(ferror(fpi))
{
fclose(fpi);
return 0;
}
fclose(fpi);
if(count == 1 || count <= linenr) return 0;
remove(fname);
rename("temp.txt",fname);
return 1;
}
 
D

dbtid

Irrwahn said:
It's unnecessary and extremely wasteful WRT memory usage to buffer
the whole file contents. Writing the new first line to a temporary
file and streaming the remainder of the original file seems much more
appropriate, especially for huge files.

I don't know why I wrote it that way. Of course you're correct.
It would be a whole lot simpler to just read up and spit out all
of the lines except for the one in question, one line at a time
into a single buffer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top