473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Editing a file at Start

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
2 2049
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: NewBob | last post by:
Since Access automatically highlights all of the text in a text control (I use it to hold data from a memo field) when the control is activated, I've added the following code to put the cursor at...
9
by: tshad | last post by:
Is there a way to use your own image in place of the automatic one that ASP uses when doing editing in your DataGrid pages? We already have a style of button we are using and would like to be...
12
by: Ron Weldy | last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net files. On my workstation I have a share set up to the folder where the web files reside. I am just doing quick and dirty...
10
by: Nathan Sokalski | last post by:
I have a DataList control with an EditTemplate. Three of the controls in this template include a Calendar, a Button with CommandName="update", and a Button with CommandName="cancel". Whenever I...
14
by: James | last post by:
I compile my code, I get a few errors, I go to the 1st error in the editor and start to type in the correct code and I get no where because with each key stroke the caret goes away, then a long...
5
by: Zytan | last post by:
I cannot stand being unable to change the code while the debugger is running. Is there a way to do this? thanks, Zytan
4
by: ovatta | last post by:
Hi! I have meke a simple programs on c++ with visual studio. The program have a dll where I put my name e my personal details. I realize that anybody can take my executable file and with a simpli...
0
by: =?Utf-8?B?TXIuIENoaXA=?= | last post by:
Greetings, This is my first post and I am hoping (praying) someone could help me. I am not a programmer, I just know enough to be dangerous. I have a custom application someone developed for...
10
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.