LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-01-2006, 07:20 AM   #1
Richie55
Member
 
Registered: Nov 2004
Location: UK
Distribution: Mandriva 2010.1
Posts: 128

Rep: Reputation: 15
C/C++ Append to begining of file


I'm trying to write a CGI scrip to upadte a file with comments, however I want the newest to appear first in the file, which is included into a html dock with php include.

However How can I append the below to the beginning of the comment file? (I'm hoping not to have to make a new file with the new comment, append the old file to the end, delete the old file and rename the new.)


out = fopen(COMMENT_FILE, "a+");

fprintf(out, "<table cellspacing=\"0\">\n");
fprintf(out, " <tr>\n");
fprintf(out, " <td class=\"time\">%d:%s:%d</td><td class=\"headline\">%s</td><td class=\"time\">&nbsp</td>\n", current_date->tm_mday, Date[current_date->tm_mon], current_date->tm_year+1900, values[nameIndex]);
fprintf(out, " </tr>\n");
fprintf(out, " <tr>\n");
fprintf(out, " <td colspan=\"3\">%s</td>\n", values[commentsIndex]);
fprintf(out, " <tr>\n");
fprintf(out, "</table>\n\n");


Thanks for the help.

Last edited by Richie55; 09-02-2006 at 02:41 AM.
 
Old 09-01-2006, 09:41 AM   #2
tamtam
Member
 
Registered: May 2004
Distribution: Slackware.
Posts: 314

Rep: Reputation: 30
no shortcut for this as far as I can tell. Open the file to read, read the file into memory, close the file. Reopen the file for writing. Write the new first record, write the remainder of the file from memory.

Hope this helps.
 
Old 09-01-2006, 09:59 AM   #3
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Alternatively:
1. Create temp file
2. Write header to temp file
3. Open old file
4. For each line in old file
Read line from old
Write line to temp
5. Close both files
6. Rename temp file to old filename
 
Old 09-01-2006, 01:26 PM   #4
Richie55
Member
 
Registered: Nov 2004
Location: UK
Distribution: Mandriva 2010.1
Posts: 128

Original Poster
Rep: Reputation: 15
Thanks for the responce,I've spent pretty much a day looking on the net for a solution, and haven't yet. I refuse to beleive that its not possible! I've come across this

int fseek(FILE *stream, long offset, int relative_to);

I've been trying to use it after: out = fopen(COMMENT_FILE, "a+"); to put the file pointer to 0, bt although it compiles with no errors or warnings, it does nothing. I'm not sure if I'm using right, or if I've missed ther point of the perpose of this.

any ideas welsome.

thanks
 
Old 09-01-2006, 01:57 PM   #5
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> any ideas welsome.

youre gonna have to use one of the two idea above, or some variation of those. even if you write to the beginning of the file its not going to magically 'shift' everything as you write
 
Old 09-01-2006, 04:28 PM   #6
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi.

I don't know of any current filesystem design that allows this. However, the design of many *nix filesystems are almost ripe for such an idea. The basic part of the FS is the inode, and the part we might be concerned is the list of data blocks that are contained in the inode. I can imagine a design that allows a special call that replaces the first pointer to a data block with the new one, and the remaining pointers shuffled down. The problems that arise are that the data might need to be padded with some representation of do-nothing-with-this-kind-of-character data, and, once you get to the indirect blocks, you really need to do some dancing.

So, in practice, writing a new file is the easiest.

However, one could also write a file that is indexed (i.e. a random file), and then you don't care where the data blocks are physically, you just update the index. So this would be like playing with the inode, but you get to do it in your space. Reading it back in requires the program to read the index and pick the data blocks in index order, but if the application is important enough, and particularly if the data collection is large enough, it's worth some extra effort.

Best wishes ... cheers, makyo
 
Old 09-02-2006, 02:39 AM   #7
Richie55
Member
 
Registered: Nov 2004
Location: UK
Distribution: Mandriva 2010.1
Posts: 128

Original Poster
Rep: Reputation: 15
Makyo, Thanks for the responce, but a little bit over my head. I think I'm going to play around with reading the origanl file into memory, and then re-write the lot. If I do come across a solution, I'll post it here. Thanks for the help guys!
 
Old 09-04-2006, 01:32 AM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by makyo
The problems that arise are that the data might need to be padded with some representation of do-nothing-with-this-kind-of-character data
And that could lead to quite a lot of disk that cannot be accessed. If an inode reserves 4Kb of disk space and you are appending a single log line (let's go for 100 bytes of data) Then you have a lot of waste, a 1Mb log file would take 40Mb of disk space.

Such an implementation would require a feature to compress the file (and free up inodes). So I would suggest that it would be easier to read the whole file in, modify it in memory and then write the modified (in this case appended) file back out.

Last edited by graemef; 09-04-2006 at 01:35 AM.
 
Old 09-04-2006, 06:28 AM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 718

Rep: Reputation: 72
Hi, graemef.

No argument there. However, if we were adding 100 MB chunks, it would look much better. Not to mention -- when was the last time anyone worried a lot about disk space? Still, your point is well-taken.

Blue-skying is useful to consider what could be done. I certainly don't have the expertise to perform even the design of that change. I had wanted to do that same user task for a long time, and, for my purposes, I wrote a prepend script to accomplish that task -- it did just as everyone agreed, but if something in *nix changes, I'll be ready to slide that new method in.

In some applications, it is useful to trade space for time -- particularly in a close-to-real-time situation so that we can provide good response at the expense of wasted space.

I have not looked deeply at current system implementations, but in the past one large system on which I did application programming expanded the size of the inodes, and placed small data files directly in the inode -- very, very clever, I thought.

I hadn't really thought about compression, but that's one of the chores that the system could do in wee small hours of the morning when the users are sleeping and the elves are working.

For the current situation, I agree, do the simple, practical thing, and keep thinking and working about could be. We need to say "I want", so that developers and vendors know our desires. Thanks for your comments, best wishes ... cheers, makyo
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
c++ append data to file blizunt7 Programming 3 07-04-2005 12:47 AM
+ at the begining of a line in password file jimieee Linux - General 3 04-03-2005 02:40 PM
> writes a new file, how to append? luwigie Linux - General 1 08-26-2004 08:42 PM
tar append file juno Linux - General 5 10-24-2002 02:59 AM
append to file d-rez Linux - Newbie 2 06-20-2002 04:55 AM


All times are GMT -5. The time now is 02:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration