I wrote a simple code:

#include <stdio.h>
#include <string.h>
main()
{
FILE *fp = fopen ("a.txt" , "rb+");
char data[200];
fgets(data, 200 , fp);
strcat(data, "Hello");
fputs(data, fp);
fclose(fp);
}

and my input file had:

1
2
3
4
5
6
7
8
9

Upon execution file a.txt had:
1
1
Hello
6
7
8
9

whereas the expected result was:
1Hello
2
3
4
5
6
7
8
9

Regards,
Prakash
----- Original Message ----
From: Nico Heinze <[EMAIL PROTECTED]>
To: [email protected]
Sent: Friday, February 1, 2008 2:09:56 AM
Subject: [c-prog] Re: Insert record in text file

--- In [EMAIL PROTECTED] com, Satya Prasad <satya_prakash_ [EMAIL PROTECTED] .>
wrote:
>
> I am trying (C program in unix) to insert data into >
> text file while reading the file in "r+" mode. The
> issue is that it overwrites the text file content
> if data to be inserted is more than the length of
> data read. 
> 
> Is there is a possible way to insert data in a text
> file without overwriting data in the file?

Nope. Text files are text files, so you can write (without huge
effort) only from beginning to end.

What you would like to do is a so-called random-access; nothing to be
done with text files. Random-access files are _usually_ handled by
using open() and read() and write() or fopen(..., "rb+") and the like.
Implementing random access on text files requires huge buffering efforts.

Regards,
Nico





      
____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  
http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 


[Non-text portions of this message have been removed]

Reply via email to