CMD window closes when I try and read from file?

This is a discussion on CMD window closes when I try and read from file? within the C Programming forums, part of the General Programming Boards category; Hey all, I'm trying to read from a file that the user has already named and written to correctly, but ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    CMD window closes when I try and read from file?

    Hey all,

    I'm trying to read from a file that the user has already named and written to correctly, but when I try to read it, the command prompt window closes. However, my code compiles successfully, and every tutorial I've looked at online is telling me to put in the same code as is in there already. Anyone got any ideas? Here's the code for reading the file:

    Code:
    void readFiles() {
    
        char choice;
        char line[80];
        int i = 0;
        printf("\nThe file \"%s\" was found. Would you like to load this file? (y/n)", filename);
        scanf("%c", &choice);
        
        if (choice == 'y' || choice == 'Y') {
        
            inFile = fopen(filename, "r");
    
            while(fgets(line, 80, inFile) != NULL) {
            
                gets(line);
                sscanf(line, "%s%d%d%d%d%d%d%d", theLeague[i].name, &theLeague[i].gamesPlayed, &theLeague[i].gamesDrawn, &theLeague[i].gamesLost, 
                &theLeague[i].goalsScored, &theLeague[i].goalsConceded, &theLeague[i].points);
                i++;
            } 
            
        }
        
        else {
        
            printf("\nYou chose not to load a file. Returning to menu...\n");
        } 
    }

  2. #2
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    Code:
    while(fgets(line, 80, inFile) != NULL) {
            
                gets(line);
    you already loaded the strings on fgets(line, 80, inFile)
    so why are you using gets again ?
    and try to put
    fflush(stdin);
    getchar();
    at the end of your source

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by St0rM-MaN View Post
    Code:
    while(fgets(line, 80, inFile) != NULL) {
            
                gets(line);
    you already loaded the strings on fgets(line, 80, inFile)
    so why are you using gets again ?
    Oh, wasn't thinking

    Quote Originally Posted by St0rM-MaN View Post
    and try to put
    fflush(stdin);
    getchar();
    at the end of your source
    i've done that, as so:

    Code:
    while(fgets(line, 80, inFile) != NULL) {
            
                sscanf(line, "%s%d%d%d%d%d%d%d", theLeague[i].name, &theLeague[i].gamesPlayed, &theLeague[i].gamesDrawn, 
                &theLeague[i].gamesLost, &theLeague[i].goalsScored, &theLeague[i].goalsConceded, &theLeague[i].points);
                i++;
                fflush(stdin);
                getchar();
            }
    but it's still crashing

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    DON'T fflush(stdin);
    read the FAQ

    How do you know 'i' is always going to be in-bounds with each iteration?
    No need to flush the stream if your using fgets()...

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by zacs7 View Post
    DON'T fflush(stdin);
    read the FAQ

    How do you know 'i' is always going to be in-bounds with each iteration?
    No need to flush the stream if your using fgets()...
    I set it to 0 at the start of the method, so that it could address each item of the struct array from zero until the end.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    I've changed the code to this for testing purposes, and it runs through, but doesn't scan the correct values in for some reason.

    Code:
    void readFiles() {
    
        char line[80];
        int i = 0;
        char fileReload[21];
            
        printf("\nEnter the name of the file you wish to load: ");
        scanf("%s", fileReload);
        
        printf("\n1\n");
                
            inFile = fopen(fileReload, "r");
            
            if(inFile == NULL) { 
               perror(NULL);
            }
            printf("\n2\n");
                
            while(fgets(line, 80, inFile) != NULL) {
            
            printf("\n3\n");
            
                sscanf(fileReload, "%s%d%d%d%d%d%d%d", theLeague[i].name, &theLeague[i].gamesPlayed, &theLeague[i].gamesDrawn,
              &theLeague[i].gamesLost, &theLeague[i].goalsScored, &theLeague[i].goalsConceded, &theLeague[i].points);
                i++;
                printf("%s", theLeague[i].name);
                
            }
        
        menu();
    }

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    scanf("%s", fileReload);
    Use fgets to read string. Dont use scanf. It would be more helpfull if you could show us your text file as well, whuch u are trying to scan.

    Is the theLeague struct variable declared as global (which isn't a good idea by anyway)??

    ssharish2005

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by ssharish2005 View Post
    Code:
    scanf("%s", fileReload);
    Use fgets to read string. Dont use scanf. It would be more helpfull if you could show us your text file as well, whuch u are trying to scan.

    Is the theLeague struct variable declared as global (which isn't a good idea by anyway)??

    ssharish2005
    The text file is in the format, for example:

    Liverpool 2 1 1 0 6 4 4
    Everton 2 0 1 1 4 6 1

    The struct is declared as global, I know it's bad and I will change it later, but for the time being I just need to get this working.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You've been working on this for a while Osiris, yeah?

    Here's your error:
    Code:
      sscanf(fileReload,
    sscanf doesn't work from a file name, (that would be fscanf() which you don't need here), it uses a string as input. Replace "fileReload", with "line". That's where your data has been placed by fgets(line, ...).

    Adak
    Last edited by Adak; 04-24-2007 at 03:36 PM. Reason: update

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21