473,398 Members | 2,368 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,398 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 2050
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.