Why when I am opening an existing file in (text) writing mode I am losing information that was written there before?
It's like creating a new file with the same name..
Thanks (:
Why when I am opening an existing file in (text) writing mode I am losing information that was written there before?
It's like creating a new file with the same name..
Thanks (:
Last edited by gavra; 12-14-2008 at 02:58 PM.
gavra.
Post your code.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
sounds like you're using the wrong mode flags when opening it, and/or not setting the file poiner correctly when writing.
To add on, that is the standard and normal behavior of opening a file in "w" mode -- you get a brand new file (anything previously there is just plain gone).
Can you show me how to open the file without creating a new one?
gavra.
I'm sure we can, but if you post your code, it will be much more appropriate to what you are actually doing - or, you could try typing "man open" or "man fopen" into a google search box - seeing as it's a fairly common thing to do.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Append to a file is probably what you want. If you are using fopen() click here . If you want to add something in the middle of a text file, you will probably have to open the file, read it, modify the data and then save it back to the original file. You can use a temporary file for saving the modified data.
So I must copy the whole content by reading and writing it into a new temporary file (and so on..)? no other option? \:
I am not posting cuz I have nothing to post (I tried to do it once and it didn't work so now I am asking.. I don't have any code).
gavra.
If your read the link and you are using fopen() then you can use the a option instead of w. That will let you continue writing from the end of the file.
If you want to modify the original and not continue writing, yes you have to copy it.
Why? Because information are sequential. Think of it as an array. If you want to insert something everything else has to be pushed ahead. Which means copied. So the copying will have to happen anyways.
More modern language could provide a method to do this for you. C doesn't. Probably because such a thing is inefficient and it will give the false impression that you can just write a character in a file without anything changing.