Thread: Reading a file.txt

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Reading a file.txt

    What am i missing in this code?

    char a;
    FILE *myfile;

    myfile = fopen("jump.txt", "r");
    a = "jump.txt";
    fscanf(myfile, &a);
    printf(" %s", a);

    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Lots of things. Maybe you'd better explain what you're actually trying to do, before we go telling you where you're going wrong.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    5

    Reading from a text file

    I created the text file "jump.txt and i want C to read the information in the .txt file, then put the output onto the screen.

    /* this will read the text file: jump.txt*\
    /* and will put it on the output screen *\
    char a;
    FILE *myfile;

    myfile = fopen("jump.txt", "r");
    a = "jump.txt";
    fscanf(myfile, &a);
    printf(" %s", a);

    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    OK, I see.

    - I wouldn't use fscanf() to do this. Try looking up fgetc() to get one character at a time.
    - a="jump.txt" is incorrect. a is only a char, and can therefore hold only 1 letter. You don't need this line anyway.
    - printf() with %s is for strings, again, a is only a single char, so it won't work.

    Pseudo code for you to follow:
    Code:
    - Open file
    - Validate its open correctly
    - While Read one character is not EOF
    - DO
    -   putchar()
    - END DO
    - close file
    There's some examples here. They're not showing this specific use, but you should be able to gain something from reading them. (look at the last example on that page)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Also, the correct way to do comments is with /* ... */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Reading file.txt - needs help
    By sanfor in forum C Programming
    Replies: 8
    Last Post: 01-19-2009, 12:53 PM
  4. Replies: 2
    Last Post: 01-28-2008, 03:07 AM
  5. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 10:36 PM