Thread: Scanning file contents to variable

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    4

    Scanning file contents to variable

    I'm trying to scan the contents of a file "phrases.txt" which at the moment is a single character 'g' and I'm trying to put that in the variable c. The problem is that this program below returns a blank character and I'm not sure why. I've even tried changing the name of the file to an invalid file name and it doesn't make a difference. I'm using the latest Dev-cpp to compile and run this if that helps. Does anyone know what I'm doing wrong? Thanks

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        char c[10];
        FILE * file = fopen("phrases.txt","r");
        fscanf(file,"%c",c);
        printf("%c",c);
        fclose(file);
    
        getch();                                       
        return 0;                                                
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    1) You need to check the file pointer returned by fopen() to be sure you successfully opened the file.

    2) Char c[10] is a character array, not a single character. So you can't use %c to load it or to display it.

    3) In your place I'd want to build a test file with perhaps 5 or 6 words in it, single characters and character strings are very different animals.

    4) The correct form for main is int main (void)

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Ah, thanks for the quick reply. I'll have a play around with it and see if anything changes

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    I've just found the problem. ;p When I was saving the text file I don't think I should have entered .txt because the file name came out phrases.txt.txt. Cheers tater!

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Porl View Post
    I've just found the problem. ;p When I was saving the text file I don't think I should have entered .txt because the file name came out phrases.txt.txt. Cheers tater!
    Believe me... you have more problems with the code than just that....

    You still need to include error checking code for fopen... and the rest.

  6. #6
    Registered User
    Join Date
    Feb 2011
    Posts
    4
    Could you help me with that? I'm not quite sure where you mean

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  2. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  3. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  4. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM