C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 18th, 2003, 02:17 AM
w2k-user w2k-user is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: athome
Posts: 50 w2k-user User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 51 m 58 sec
Reputation Power: 7
Question fprintf - file input

hi,

i know i'm really a beginner in ANSI C. I have to fill in a textfile with logs. The problem is that the last input is the last line in the textfile.

I want that the last input is the first line? How can I achieve this the easiest way? To read everything out and fill it in afterwards would be quite unefficient?

cu
EP

Reply With Quote
  #2  
Old March 18th, 2003, 03:12 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
Hi,

As far as I know, you can only do two things when writing to a file: you can add to the end of the file, or you can overwrite something in the file.

Reply With Quote
  #3  
Old March 18th, 2003, 03:29 AM
w2k-user w2k-user is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: athome
Posts: 50 w2k-user User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 51 m 58 sec
Reputation Power: 7
fprintf

i know that i can only open files with serveral flags, which only allow appending info or deleting the file and inserting new info.

but i need a short algorithm which allows inserting lines at the beginning and doesn't delete the other lines.

thx in advance
cu
EP

Reply With Quote
  #4  
Old March 18th, 2003, 05:15 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,327 7stud User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 44 m 50 sec
Reputation Power: 9
"To read everything out and fill it in afterwards would be quite unefficient?"

My point was, as far as I know, you don't have any other options.

It doesn't sound very hard. You could read the current lines of the file into strings, then write the current log to the file, and subsequently write the strings back to the file.

Reply With Quote
  #5  
Old March 18th, 2003, 09:53 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,803 dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)dwise1_aol User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 1 Month 11 h 57 m 9 sec
Reputation Power: 437
Or another kludge would be:
1. create a new file.
2. write the new log entry to it. Keep the new file open.
3. open the log file for read.
4. read each line of the log file and write it to the new file.
5. close both files.
6. delete the log file
7. rename the new file to the log file's name.

Instead of deleting the log file, you could keep a series of backup copies just in case, similar to Linux's messages log files -- messages, messages.1, messages.2, messages.3, messages.4 etc.

In that case, steps 6 & 7 would be:
6. delete the oldest log-file back-up (eg, log.4)
7. rename the next-oldest to the oldest's name
(e.g., rename("log.3","log.4");
8. similarly bump the names of the other log files:
log.2 -> log.3
log.1 -> log.2
log -> log.1
9. now rename the new file to the log-file name.

Not very elegant, but it should work.

Reply With Quote
  #6  
Old March 18th, 2003, 11:19 AM
w2k-user w2k-user is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: athome
Posts: 50 w2k-user User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 51 m 58 sec
Reputation Power: 7
thx

thanks for your advice.
was quite a help for me.

cu
w2k-user

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > fprintf - file input


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway