The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
insert some text to a file log bucci Shell Programming and Scripting 4 05-09-2007 07:19 AM
insert text into top of file jimbob Shell Programming and Scripting 1 09-22-2006 05:46 PM
Insert text file at a certain line. insania Shell Programming and Scripting 4 08-01-2006 02:46 AM
SED- Insert text at top of file MBGPS Shell Programming and Scripting 12 07-03-2002 09:48 AM
insert text into a dinamic file sunbird UNIX for Dummies Questions & Answers 2 11-05-2001 10:58 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2008
netwolf netwolf is offline
Registered User
  
 

Join Date: May 2008
Posts: 2
Question Insert Text at begining of file

Hi All,

Its been a long time since i did any programming and I am trying to get back into it as part of also learning Linux. Coming from a Cpp background i am trying to write some simple code just using C.

I want to create a simple log file but instead of appending to the end of the file i want to prepend it at the start. Try as i might i cant find any references to prepending text to a file.

I have tried rewind and fprintf to get to the top of the file but it overwrites existing entries at the top. How can i insert the text. Secondly I have read it is better for portability to use the Low Level I/O functions like open() read(), write and creat(). Can some one give me an example using the buffered methods like fopen, fprintf streams and the unbuffered methods like open() write() etc. I have read that fopen actually calls open() to open the file but provides a buffered stream.

I would appreciate your help very much. The books i have dont seem to go into much detail and though I have found many examples of reading and writing they dont seem to cover how to prepend. I suppose i could create a new file, but this seems to be a long way around the problem.

Thanks in advance and Regards
  #2 (permalink)  
Old 05-27-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
There are a lot of ways. Here is one.

1 open a temp file. Write the new lines to it.
2. open the log file - read the lines from the log ; write them to the temp file
3. close both files
4 rename the temp file as the logfile.
  #3 (permalink)  
Old 05-27-2008
netwolf netwolf is offline
Registered User
  
 

Join Date: May 2008
Posts: 2
Jim,

Thanks, I had considered that but it seems to me a round about way. I was hopping for a more direct approach.

Regards
Netwolf
  #4 (permalink)  
Old 05-27-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Another possibility:

1 read the contents of the file in a buffer
2 write the new lines to the file
3 write the buffer to the file

Regards
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0