problem appending

This is a discussion on problem appending within the C++ Programming forums, part of the General Programming Boards category; It would appear, based on the error message, that you have accfile defined at the outer scope as being an ...

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It would appear, based on the error message, that you have accfile defined at the outer scope as being an ifstream, and you still can't write to an ifstream.

  2. #17
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Anything that is declared after an opening brace has a lifetime that lasts until the corresponding closing brace and then goes "out of scope". So you cannot use accfile outside the scope where it was declared.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #18
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    All the errors have been sorted.

    I am now having a problem with only the latest pieces of data that I output being saved in the file. Whatever else was in there vanishes. From examples that I've looked at, it should be working.

    Am I ouputting to the file wrong? How do I correctly append the file?

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,705
    Quote Originally Posted by Furious5k
    I am now having a problem with only the latest pieces of data that I output being saved in the file. Whatever else was in there vanishes. From examples that I've looked at, it should be working.

    Am I ouputting to the file wrong? How do I correctly append the file?
    You should open the file with std::ios::app as the mode flag.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Code:
    #include <iostream>
    using namespace std;
    If I'm using those, I don't need to write the "std::ios::" part?

    Do I write this?
    Code:
    accfile.setf(app);

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,705
    Quote Originally Posted by Furious5k
    If I'm using those, I don't need to write the "std::ios::" part?
    You would still need to qualify with ios.

    Quote Originally Posted by Furious5k
    Do I write this?
    More like:
    Code:
    ofstream accfile(filename, ios::app);
    or:
    Code:
    accfile.open(filename, ios::app);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Could anyone point me in the right direction, I'm struggling to figure out/find example of how I can erase a line of text from a txt file?

  8. #23
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can't. You can copy the file or the parts you want to keep.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #24
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Alright, how would I go about doing that?

    Would I just read everything into an array and then write it back skipping the unwanted bits? The only thing I found in tutorials is "ios::trunc", would I write the following before writing everything (minus unwanted bits) to the file to get an updated version?

    Code:
    accfile.open(filename, ios::trunc, ios::app);

  10. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You do not want ios::trunc, ios::app - you either want to append, or you want to truncate (which makes the file zero length) - and if you want to combine such attributes, they should be combined with | not a comma.

    Yes, to remove a something in a file, you have two choices:
    • Read all the file into memory (an array or some such), create a new file with the same name [do not crash at this point, or all data is lost!], write back out the bits you actually want to keep [of course, you can "not store them in memory" if that is easy to determine when you are reading the file into memory].
    • Create a temporary file, read from the input file, write the data (except the bits you want to remove) to the temporary file. Remove the old file, then rename the temporary file to the original name.

    You can obviously do the first variant by writing the new data to a temporary file, then removing the original and renaming the temporary file, which avoids the "all is lost if you crash here".

    --
    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.

  11. #26
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Is the code for creating/removing/renaming files difficult? (working on a 10" netbook during holidays frustrated me into giving up until I got home, so I'm a bit tight on the deadlines now)

  12. #27
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Furious5k View Post
    Is the code for creating/removing/renaming files difficult? (working on a 10" netbook during holidays frustrated me into giving up until I got home, so I'm a bit tight on the deadlines now)
    Look up the furnctions remove() and rename() - they are not very hard to use.

    --
    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.

  13. #28
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Code:
           ofstream accfile;
           accfile.open ("loans.txt", ios::trunc | ios::app);
    
           string book;
           cin >> book;
    
           for (int i = 0; i < (BOOK_MAX+1); i++)
             {
               if (book == Loans[i].Book)
                 {
                   accfile << Loans[i].User << ";" << Loans[i].Book;
                 }
             }
           accfile.close();
    I'm getting a "Segmentation fault", which is odd since I'm outputting much the same as I did before. (I made a copy of the file for crashes, thanks for the pointer).
    Last edited by Furious5k; 01-06-2009 at 06:19 AM.

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,705
    I agree with matsp: ios::trunc | ios::app does not make sense. In The C++ Standard Library: A Tutorial and Reference Josuttis states on page 632 that "other combinations not listed in the table, such as trunc|app, not allowed".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Quote Originally Posted by laserlight View Post
    I agree with matsp: ios::trunc | ios::app does not make sense. In The C++ Standard Library: A Tutorial and Reference Josuttis states on page 632 that "other combinations not listed in the table, such as trunc|app, not allowed".
    But he said:
    and if you want to combine such attributes, they should be combined with | not a comma.

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 04:46 PM

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