Thread: problem with file handling

  1. #1
    Registered User
    Join Date
    Aug 2014
    Posts
    47

    Question problem with file handling

    I just try to make a simple program that is supposed to append some int value in a txt file. But my code doesnt work and i dont know why. I try it earlier and it did work but it wont in my 2nd time around. Please help me. i am using C v5.7.1 on windows 8. Thanks.

    Code:
    #include<stdio.h>
    main ()
    {
      int a=100;
     FILE *fp;
    fp=fopen("score.txt",  "wa");
    if (fp==NULL)
    { 
       printf("file not found");
    }
    fprintf(fp, "%d", &a);
    fclose(fp);
    
    return 0;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Compile with warnings turned up to a high level. The compiler should be warning you of a problem with your fprintf call that you should fix.

    Also, write main() as int main(void). It is good that you checked if fp is a null pointer, but you should not call fprintf if fp is a null pointer.
    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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    It's int main() -- you need to list the return type explicitly.

    Read the documentation for fopen. "wa" is not a valid mode. You can use "w" or "a", but it doesn't make sense to use both, and per the standard, it results in undefined behavior, meaning anything or nothing can happen:
    Quote Originally Posted by C99 7.19.5.3 p3
    The argument mode points to a string. If the string is one of the following, the file is
    open in the indicated mode. Otherwise, the behavior is undefined.231)
    r open text file for reading
    w truncate to zero length or create text file for writing
    a append; open or create text file for writing at end-of-file
    rb open binary file for reading
    wb truncate to zero length or create binary file for writing
    ab append; open or create binary file for writing at end-of-file
    r+ open text file for update (reading and writing)
    w+ truncate to zero length or create text file for update
    a+ append; open or create text file for update, writing at end-of-file
    r+b or rb+ open binary file for update (reading and writing)
    w+b or wb+ truncate to zero length or create binary file for update
    a+b or ab+ append; open or create binary file for update, writing at end-of-file

    231) If the string begins with one of the above sequences, the implementation might choose to ignore theremaining characters, or it might use them to select different kinds of a file (some of which might not
    conform to the properties in 7.19.2).
    If fopen fails, you should use a function like strerror to print out a useful message telling you why fopen failed (maybe file not found, maybe the device was busy, don't have permissions, etc). You should probably return immediately from main after this -- if you can't open the file, you can't write to it, so there's no point carrying on. In fact, trying to write to, or close, a null FILE * will result in undefined behavior.

    Lastly, why are you printing the address of a, i.e. why are you printing &a? If you want to print the address, the correct format specifier is %p. If you want to print the value, drop the &.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Handling problem
    By programmerc in forum C Programming
    Replies: 1
    Last Post: 03-11-2013, 12:25 PM
  2. problem in file handling
    By puneet01 in forum C++ Programming
    Replies: 5
    Last Post: 06-06-2011, 05:02 AM
  3. File Handling problem, please help
    By alice06 in forum C Programming
    Replies: 1
    Last Post: 04-07-2011, 10:06 PM
  4. Problem with handling a file
    By raghuveerd in forum C Programming
    Replies: 2
    Last Post: 06-19-2007, 02:42 AM
  5. Problem in file handling.
    By Abhid in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:11 AM