Thread: problem appending

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I tried to seperate them, like so, but it still didn't work (same segmentation fault).
    Code:
           ofstream accfile;
           accfile.open ("loans.txt", ios::trunc);
           accfile.close();
    
           accfile.open ("loans.txt", ios::app);

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Furious5k View Post
    But he said:
    Yes, I said that - but I also said "Do not combine app with trunc" as you are at the same time saying "truncate the file" and "append to the file" - which makes absolutely no sense - and what you actually get from it is completely undefined.

    What I wanted to tell is that IF you have valid combinations that you want to pass in, you need to combine them using the bitwise or operator.

    Did you actually test if accfile is valid before writing to it? Such as
    Code:
    if (!accFile) {
        cout << "Could not open the file... " << endl;
        return;
    }
    It may well be that the iostream implementation actually detects the bad combination and the opening of the file fails, perhaps?

    I am also a bit suspicious about this one:
    Code:
           for (int i = 0; i < (BOOK_MAX+1); i++)
    Why is there a +1 there?

    --
    Mats

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

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, 11: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, 05:46 PM