Need help writing to file

This is a discussion on Need help writing to file within the C Programming forums, part of the General Programming Boards category; I need help writing to a file. I can't get it to work but its not giving any errors. Code: ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    20

    Need help writing to file

    I need help writing to a file. I can't get it to work but its not giving any errors.
    Code:
    #define OUTPUT "output.txt"
    ...
    out = fopen(OUTPUT, "w");
    if(!out) fatal("Could not open %s for writing\n", OUTPUT);
    fprintf(out, "Got Here");
    There is no compile error, there is a segmentation fault, but that occurs later in the code. I have print statements that get printed before the segmentation fault. After i run the code, the message "Got Here" does not get printed to output.txt. I also open another stream for writing later in the code, but it is after all this happens, and it does not work either. Any help is appreciated.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In all likelihood it's the segfault causing the problem.... If your files are still open when it occurs the data will be in memory buffers that probably don't get written to disk. Try closing the file immediately after your fprintf() call and see what happens.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by dford425 View Post
    There is no compile error, there is a segmentation fault, but that occurs later in the code.
    Which means it will be hard to say what is causing it based on what you have posted.

    Quote Originally Posted by CommonTater View Post
    In all likelihood it's the segfault causing the problem.... If your files are still open when it occurs the data will be in memory buffers that probably don't get written to disk. Try closing the file immediately after your fprintf() call and see what happens.
    I agree, fflush() should work as well.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    20
    Thanks for the quick replys, fflush() worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing to file
    By renvaar in forum C Programming
    Replies: 3
    Last Post: 03-18-2010, 02:51 PM
  2. Replies: 16
    Last Post: 04-20-2009, 04:33 PM
  3. Replies: 3
    Last Post: 11-21-2006, 06:26 PM
  4. writing to file
    By Mr.Pink in forum C++ Programming
    Replies: 6
    Last Post: 02-17-2005, 03:33 PM
  5. reading from file and writing in a nother file
    By undisputed007 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2004, 01:17 PM

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