Thread: Switching file name

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

    Switching file name

    So something really weird is happening to my program for number 1. It compiles without warning but for some reason the filename gets changed to whatever string the function was counting so for the final print statement it prints "(whatever last string was in file) contains (however many words were in the file)" .. Anyone have any idea why?
    insert
    Code:
    
    
    Code:
    #include<stdio.h>
    #include<string.h>
    
    
    int main()
    {
    FILE *fp;
    char f_name[64];
    char status;
    int count = 0;
    char x;
    
    
            printf("Enter the file name: ");
            scanf("%s", f_name);
    
    
            printf("%s\n", f_name);
            fp = fopen (f_name, "r");
            do
            {
            x = fscanf(fp,"%s", &x);
            status = x;
            if (status != EOF)
                    {
            count++;
            printf("%s\n", f_name);
                    }
            }
            while ( status != EOF );
            printf("%s contains %d words.\n", f_name, count);
    
    
            return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    What does the file look like?

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    Quote Originally Posted by new2C- View Post
    What does the file look like?
    It's just a standard txt file with a random assortment of words that the program counts.

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    Your fscanf is reading a string but only storing it in a char, (x). You should have a string to store the values.

    x should be of type int I believe, EOF is int.

  5. #5
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    Will that fix my problem with it switch what f_name is? because the program does what it's supposed to except it switched f_name to whatever the last string was.

  6. #6
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    All you are doing is printing out the file name, you arent printing anything out from the file itself. I think you are experiencing some undefined behavior.

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    It prints out how many words were in the file.

  8. #8
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    Code:
    x = fscanf(fp,"%s", &x);//x should be of type int, and the &x should be a string
    
    //for example
    int x,status;
    char array[256];
    x = fscanf(fp,"%s", array)

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    5
    That fixed it! Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Win32 File Switching within a directory
    By hemanth.balaji in forum Windows Programming
    Replies: 1
    Last Post: 06-10-2005, 10:20 AM
  2. Switching to RGB from GRB
    By HQSneaker in forum Game Programming
    Replies: 20
    Last Post: 09-28-2004, 07:48 AM
  3. switching between players
    By librab103 in forum C Programming
    Replies: 3
    Last Post: 06-24-2003, 03:10 PM
  4. Switching streams?
    By <<>> in forum C++ Programming
    Replies: 4
    Last Post: 11-06-2002, 09:56 AM
  5. switching
    By iain in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-11-2001, 08:29 AM

Tags for this Thread