Thread: problem appending

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, if you want to combine such attributes (e.g., ios::in | ios::out), but you cannot combine those two.
    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. #32
    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);

  3. #33
    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.

  4. #34
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Furious5k
    I tried to seperate them, like so, but it still didn't work (same segmentation fault).
    The segmentation fault might have some other cause. For example, how many elements does Loans have?

    Note that separating them the way you did just makes the first opening of the file have no net effect. You basically opened, did nothing, then closed.
    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

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by laserlight View Post
    Note that separating them the way you did just makes the first opening of the file have no net effect. You basically opened, did nothing, then closed.
    Well, in the sequence as described here, it truncates the file, so it does indeed have an effect - it removes the conent in the file. Why you would THEN append to the file, I don't really understand, as there is no content to append onto - so the second file opening is absolutely meaningless waste of time [as long as the close is removed]. When the file is truncated, it has a zero length, the write (put) pointer is at the beginning of the file, and the file is opened for write - so what does the open with ios::app actually do at this point?

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

  6. #36
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    matsp - due to my lack of experience. I think it should be -1. An array[5] would be 0 to 4, yes?

    Also, sorry for misreading.

    laserlight - I was under the impression that opening the file with ios::trunc would erase its contents. How do I do this properly?


    On another note, would this sort a "string myaray[NUMBER]" alphabetically?
    Code:
    #include <algorithm>
    sort(myarray, myarray+NUMBER)
    Last edited by Furious5k; 01-06-2009 at 07:57 AM.

  7. #37
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Does renaming a temporary file make it permanent?

  8. #38
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Furious5k
    laserlight - I was under the impression that opening the file with ios::trunc would erase its contents. How do I do this properly?
    What are you trying to do?
    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

  9. #39
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    I have read the file into an array earlier on in the code.
    Now I want to erase the contents and re-enter them from the array skipping the location in the array which holds the same content as the one entered by user

    The alphabetical sort is for another part of the code. I probably shouldn't have posted it here, sorry for the confusion.

  10. #40
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Furious5k View Post
    matsp - due to my lack of experience. I think it should be -1. An array[5] would be 0 to 4, yes?
    That is probably causing your segmentation fault then - and it probably should be
    Code:
    for (int i = 0; i < BOOK_MAX; i++)
    No +1 or -1.
    Also, sorry for misreading.

    laserlight - I was under the impression that opening the file with ios::trunc would erase its contents. How do I do this properly?
    It does - but you do not then need to use ios::app on the file to add to it - since it is already empty.


    On another note, would this sort a "string myaray[NUMBER]" alphabetically?
    Code:
    #include <algorithm>
    sort(myarray, myarray+NUMBER)
    [/QUOTE]

    I suppose so.

    --
    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. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Furious5k View Post
    Does renaming a temporary file make it permanent?
    No - all files are always permanent. Renaming a file changes it's name from one name to another. When I said temporary file, I meant something like "tmp.dat", and then rename it to "books.dat" or whatever the "real" name of the file is.

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

  12. #42
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    It's working now. But when I cat the contents of the file I get the content I want plus a great many ";". I know which part of the code it is.

    How would I tell the code to check for null value (never done it)?
    Code:
    if (Loans[i].Book = NULL)
    That's how I understand it.


    With the temporary files, I was referring to another site I looked at, which defined "tmpfile" as "Open a temporary file". I guess it just creates a file then, which is (usually) used temporarily?

  13. #43
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Furious5k View Post
    It's working now. But when I cat the contents of the file I get the content I want plus a great many ";". I know which part of the code it is.

    How would I tell the code to check for null value (never done it)?
    Code:
    if (Loans[i].Book = NULL)
    That's how I understand it.
    That is incorrect, it is checking
    Code:
    if ((Loans[i].Book = NULL) != 0)
    , and since NULL == 0, it will never be true.

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

  14. #44
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    ...so you need == instead of =

  15. #45
    Registered User
    Join Date
    Dec 2008
    Posts
    66
    Nevermind, thank you C_ntua.

    But I now receive an error " no match for `string & == int' "

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