R
ritesh
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
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