![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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. |
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|