Files

This is a discussion on Files within the C Programming forums, part of the General Programming Boards category; Why when I am opening an existing file in (text) writing mode I am losing information that was written there ...

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    Files

    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 01:58 PM.
    gavra.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    sounds like you're using the wrong mode flags when opening it, and/or not setting the file poiner correctly when writing.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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).

  5. #5
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Can you show me how to open the file without creating a new one?
    gavra.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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.

  8. #8
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    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.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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.

  10. #10
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Thank's dude (=
    gavra.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  2. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21