By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,843 Members | 864 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,843 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
--------------------------------------------------------------------------------------------------------------------------------------------|
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Amit Kulkarni wrote:
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
Nov 14 '05 #2

P: n/a
dbtid <db***@dev.null.com> wrote:
Amit Kulkarni wrote:
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?
<snip>
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
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #3

P: n/a


Amit Kulkarni wrote:
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;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4

P: n/a
Irrwahn Grausewitz wrote:
dbtid <db***@dev.null.com> wrote:
Amit Kulkarni wrote:

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?

<snip>
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.


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.
<snip>

Regards

Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.