Thread: Help me find the problem in a very simple program

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    52

    Help me find the problem in a very simple program

    So, I'm rather new to reading and writing files. I have the following program:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int number;
        FILE *num;
        num=fopen("file directory\num.dat","r");
        fscanf(num,"%d",&number);
        printf("Read number: %d\n",number);
            
        fclose(num);
        return 0;
    }
    I also have a file named num.dat that contains a single number, 100. However, the printf only prints 0 as the read number. What am I doing wrong?

    EDIT: Obviously, "file directory" in the above code is the actual file directory in my pc.
    EDIT2: Problem solved, I needed to add double backslashes. Thanks everyone
    Last edited by Sotos; 05-29-2015 at 08:44 AM.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    1. "save" is undeclared, so your program does not even compile. It looks like you meant to use "num" on lines 7 and 8.
    2. You should check that the file has opened successfully, before trying to access it.
    3. If you did this, you might realize your string is not quite correct ... you need to escape the escape char (\\)

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    52
    Uh, yeah, the file is actually named save, but I rewrote the code to avoid giving context. Seems I missed that part.

    Thanks for the advice, that was indeed my problem, and I also wrote a line to check if it opened correctly. Thanks

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    When using the backslash character "\" you need to double them up. In your example, the compiler sees your filename as:

    Code:
    file directory
    num.dat
    This is because '\n' is interpreted as a new line character. Change it to "file directory\\num.dat" and it should work better, or lead you to your next problem to solve.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you do not check return values of functions you are calling - so you never know what failed and why...

    Add some error checking to your code

    if you call some library function - read the section "Return values" in function reference description - and use this to check for possible failures
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in Program to find Transpose of Given Square Matrix
    By nazanin.padekan in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2013, 12:51 PM
  2. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  3. simple bash program using find
    By Arical in forum Linux Programming
    Replies: 2
    Last Post: 09-21-2006, 03:26 PM
  4. Math Equation Program (I can't find the problem with my program!)
    By masked_blueberr in forum C Programming
    Replies: 14
    Last Post: 07-06-2005, 11:53 AM
  5. Replies: 4
    Last Post: 08-15-2002, 11:35 AM