Thread: Open a file, read it, display it and process char.

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    3

    Open a file, read it, display it and process char.

    Hello,

    I am trying to open a file, read the file,display the characters on the screen and process the data for stats.

    I am able to open the file, read the file and then it is either display it or just process the data.

    if i remove this part ,

    /* Read one line of text */
    fgets( c, 100, pFile );
    /* Print it to stdout */
    printf( "%s", c );


    line 32 and 34, i am able to process the data.

    Please note - I am able to read from a text file that is in the same folder as my source code. I called the file "text.txt" and i just type some thing inside it. Make sure the letter q is included. q acts like the end of file.I have tried EOF but does not work. I don't know why. let me know if you know why.

    Where is the bug?

    Thanks for your help

    Code:
    /* SS7q3 - Programming Exercise 11-11 modified */
    #include <stdio.h>
    #include <ctype.h>       // for character testing 
    #include <stdbool.h>     // for boolean
    #define STOP 'q'     
    int main(void)
    {
       char c[100];               // read in character 
       char  filename[100];
       int ch;
       int n_words = 0;      // number of words
       int n_upper = 0;        // number of uppercase characters 
       int n_lower = 0;       // number of lowercase characters     
       int n_punc = 0;      // number of punctuation marks     
       int n_digit = 0;       // number of digits          
       FILE * pFile;
       bool inword = false;  // == true if c is in a word  
              
       
       printf("Enter the filename to be analyzed\n");
       gets(filename); 
       pFile = fopen(filename, "r" );
    
      if (!pFile)
          fprintf( stderr, "I couldn't open the file.\n" );
       
    /* Programs gets stuck with the code below*/
    else
    {
        
         /* Read one line of text */
       fgets( c, 100, pFile );
        /* Print it to stdout */
       printf( "%s", c );
         
       while ((ch = getc(pFile)) != STOP)
       {    
            if (!isalpha(ch) && !inword)
            {
             inword = true;  // starting a new word 
             n_words++;      // count word          
            }
            if (isalpha(ch) && inword)
             inword = false; // reached end of word
             
            if (isupper(ch))
               n_upper++;
            else if (islower(ch))
               n_lower++;
            else if (ispunct(ch))
               n_punc++;
            else if (isdigit(ch))
               n_digit++;
          }
    }
       
       printf("\nwords = %d, uppercase = %d,  lowercase = %d, "
              "punctuation = %d, digits = %d\n",
               n_words,n_upper,n_lower, n_punc, n_digit);
               
       fclose(pFile);        
               
          /* Pause program to view result */  
        printf("\nPress [Enter] to exit, Bye.\n\n");         
        fflush(stdin);
        getchar();   
        
       return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If I'm understanding your complaint, once you've read that first line, you can't read it again unless you rewind the file back to the beginning.

    I have no idea how you were testing end of file. You should just change STOP to EOF.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Call fgetpos() before fgets() and fsetpos() before getc() to move the file offset back by the no. of bytes read.
    Alternatively, you can roll your own version of getc() which reads a character at a time from the buffer c[100].

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    i somehow cannot use EOF in this program. It just does not work. the end of file in my case is set once the character q is encounter.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If I take your program, change the word STOP to the word EOF, and compile it and run it, it works.
    Code:
    Enter the filename to be analyzed
    ss7q3.c
    #include <stdio.h>
    
    words = 135, uppercase = 15, lowercase = 501, punctuation = 184, digits = 15
    You would get different results because I didn't put in the comments.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    I finally got it woking using the rewind function, even with the EOF.

    The definition of a word here is

    "a word is defined to be any string of letters or numbers not separated by spaces or punctuation. For example, "hello" and "321" are each a single word, but "555-1212" and "C1A 4P3" are each two words"

    Code:
    /* SS7q3 - Programming Exercise 11-11 modified */
    #include <stdio.h>
    #include <ctype.h>       // for character testing 
    #include <stdbool.h>     // for boolean   
    int main(void)
    {
       char c[100];               // read in character 
       char  filename[1];
       char ch;
       int n_words = 0;           // number of words
       int n_upper = 0;           // number of uppercase characters 
       int n_lower = 0;           // number of lowercase characters     
       int n_punc = 0;            // number of punctuation marks     
       int n_digit = 0;           // number of digits          
       FILE * pFile;
       
       bool inword = false;       // == true if c is in a word  
              
       
       printf("Enter the filename to be analyzed\n");
       gets(filename); /* supplied  file is text.txt or make sure file has |*/ 
       pFile = fopen(filename, "r" );
    
       if (!pFile)
       fprintf( stderr, "I couldn't open the file.\n" );
       
       else
        {
            fgets( c, 100, pFile ); /* Read text */
            printf( "%s", c ); /* Print it to stdout */
        }
        
        rewind(pFile); /* Move pointer back to iniial position*/
        while ((ch = getc(pFile)) != EOF)
    
       {    
            if (isalpha(ch) && !(inword) )
            {
             inword = true;  // starting a new word 
             n_words++;      // count word          
            }
            if ((isspace(ch) || ispunct(ch)) && (inword))
             inword = false; // reached end of word
             
            if (isupper(ch))
               n_upper++;
            else if (islower(ch))
               n_lower++;
            else if (ispunct(ch))
               n_punc++;
            else if (isdigit(ch))
               n_digit++; 
       }
       
       printf("\nwords = %d, uppercase = %d,  lowercase = %d, "
              "punctuation = %d, digits = %d\n",
               n_words,n_upper,n_lower, n_punc, n_digit);
               
       fclose(pFile);        
               
          /* Pause program to view result */  
        printf("\nPress [Enter] to exit, Bye.\n\n");         
        fflush(stdin);
        getchar();   
        
       return 0;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you are testing characters for EOF then you need an int, not a char.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed