I need to know if a file has some data and I need to add a new value from the top. How can I do that?
I need to know if a file has some data and I need to add a new value from the top. How can I do that?
I'm not quite sure what you necessarily want to do, but if you just wanted to check if a file has data in it, you could just check to see if its first character is EOF. If it is, it has nothing, if it isn't its got data.
Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction
It seems like a safe way to do it would be to open a new file.
Write the new line to the new file.
Write the contents of the current file to the new file.
Then replace the current with the new.
You could just read all of the current file into memory, and then rewrite the file. (the new line plus what you've stored), but there's more chance for something going wrong and destroying the file.
Check out remove() to delete the old file and rename() to rename the new file to the old file name.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.