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

Editing a file at Start

P: n/a
Hi,

I'm facing a problem in which I need to edit an already created file,
and the editing needs to be done at the start of the file rather then
appending to the file.

OS - Linux,Solaris

For e.g.
I have a file test.txt created and I have the path to this file in a
(char *) string.

------------------------------ CODE START --------------------------

char * filePath[128] = getFilePath(); //implementation of this
function is not relevant
FILE * filePtr = fopen(filePath, "at"); //open the file in append mode

/* the filePtr is currently pointing to the end of the file, from where
onwards I can append data to this file. However I need to insert some
text at the beginning of this file, so I try this - */

rewind(filePtr); //just to make sure that the filePtr points to the
end of the file
struct stat file;
stat(filePath, &file);
long int length = file.st_size; //get the size of the text file
fseek(filePtr, -length, SEEK_SET); //move backwards by an offset equal
to the size of the file

------------------------------ CODE END --------------------------

It seems that the last statement above dosen't work as I expected,
since all writes to the file still appear at the end of the file.

Also using "struct stat" makes the code non-portable. I know that
getting the size of a file using the stat calls is not a good way, but
it works for me right now.

Isn't there a Standard C function which allows writting to a file at
any location rather then append only mode. If I open the file using
the write ("wt") mode then the existing file is truncated and I loose
all data.

I can also go for a non-standard/non-portable method that works on
linux.

Thanks,
Ritesh

Jul 13 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ritesh wrote:
Hi,

I'm facing a problem in which I need to edit an already created file,
and the editing needs to be done at the start of the file rather then
appending to the file.

OS - Linux,Solaris

For e.g.
I have a file test.txt created and I have the path to this file in a
(char *) string.

------------------------------ CODE START --------------------------

char * filePath[128] = getFilePath(); //implementation of this
function is not relevant
1. I beg to disagree; does this function return a pointer to
char? Then you should have

char *filepath = getFilePath ();
FILE * filePtr = fopen(filePath, "at"); //open the file in append mode
2. You don't check if the file was opened properly or if
fopen() returned NULL; what in case the file was not found?
check the return from fopen before using it.

3. What is "at"? By default, your system should open an input
file in text mode anyway, but the "t" is not proper C.
/* the filePtr is currently pointing to the end of the file, from where
onwards I can append data to this file. However I need to insert some
text at the beginning of this file, so I try this - */

rewind(filePtr); //just to make sure that the filePtr points to the
end of the file
4. This will set the file pointer topthe beginning of the file,
not the end.
struct stat file;
stat(filePath, &file);
long int length = file.st_size; //get the size of the text file
5. The above 3 lines are not C. To get the size of a file in C
try something like this:

long position;
fseek (filePtr, 0L, SEEK_END);
position = ftell (filePtr);

A better method is to just read in every byte in the file
and keep a count of the bytes read in.
fseek(filePtr, -length, SEEK_SET); //move backwards by an offset equal
to the size of the file

------------------------------ CODE END --------------------------

It seems that the last statement above dosen't work as I expected,
since all writes to the file still appear at the end of the file.
What did you think "append" means? All writes are done to the end
of the file.
Also using "struct stat" makes the code non-portable. I know that
getting the size of a file using the stat calls is not a good way, but
it works for me right now.
If time is not an issue, read the file in byte by byte to get
the file size.
>
Isn't there a Standard C function which allows writting to a file at
any location rather then append only mode.
No. Further, I don't know of any /non-standard/ function either.
If I open the file using
the write ("wt") mode then the existing file is truncated and I loose
all data.
You "lose" the data, not "loose" the data[1]. What I suggest
is this:

1. Read the file in a line (or a char) at a time *until*
you get to the place you want to insert/modify data.
Store all the lines/chars you read in to a different
temporary file.
2. At that point write *your* changes to the temporary
file.
3. Continue reading from file and storing in temporary file
until no more data.
4. Rename the temporary file to the original filename.

TADA! your changes are done without you even needing the
filesize *and* your code is portable.
>
I can also go for a non-standard/non-portable method that works on
linux.

note:
[1] Sorry but this annoys mean intensely, but I did try
to reduce the pain of inflammatory remarks by trying to
help :-)
goose,

Jul 13 '06 #2

P: n/a
"goose" <ru**@webmail.co.zawrote:
ritesh wrote:
I'm facing a problem in which I need to edit an already created file,
and the editing needs to be done at the start of the file rather then
appending to the file.
Read the FAQ: <http://www.c-faq.com/stdio/fupdate.html>. (In short, it
may not be possible for text streams, and may not be as straightforward
as you think it is for binary streams.)
struct stat file;
stat(filePath, &file);
long int length = file.st_size; //get the size of the text file

5. The above 3 lines are not C. To get the size of a file in C
try something like this:

long position;
fseek (filePtr, 0L, SEEK_END);
position = ftell (filePtr);
Read the FAQ: this is not required to work for either binary _or_ text
streams. <http://www.c-faq.com/osdep/filesize.html>.

Richard
Jul 13 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.