Thread: Reading # of characters and words from a file

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    17

    Reading # of characters and words from a file

    It works for one or the other, but not both. If I put in both sections of code (character count and word count) it will have the word count return 0 to the counter. If I take one away, they work fine! I will put it in separate functions when im done..any ideas of what is wrong??

    here is the code

    Code:
    FILE *fp;
    while (1){    char filename[100];
       
        puts("Enter the name of the file to open");
    
    
        scanf("%s", &filename); 
    
    
        fp=fopen(filename, "r");
     
    
    
         
              if (fp == 0)
                {
                  printf("\n\n%s file name not found!\n\n", filename); 
                } 
            
              else 
              {
                puts("File successfully opened");
                break; 
              }
    
    
      
       
    }//End while loop
    //////////////////////////////////////
    
    
    //charcount function
      
    
    
       while (1) {
            ch = fgetc(fp);
           ++charCount;
     
            if (ch == -1)
                break;
              }
    ch = 0;
             
      
     
    
    
    printf("\nNumber of characters: %i\n", charCount);
        
    
    
    
    
    
    
    
    
    //////////////////////////////////////////////////////
    
    
    //WORDS COUNTER FUNCTION
    
    
     
      
     while((i = getc(fp)) != EOF)
           {  
    
    
    if ((i = getc(fp)) == ' ')
    ++wCount;
     
     }
      printf("Number of words: %i\n", wCount);

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    When you run both functions, the second one is starting at the end of your file. Rewind the file pointer between functions.

    Also, your indentation is ........-poor.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Quote Originally Posted by memcpy View Post
    When you run both functions, the second one is starting at the end of your file. Rewind the file pointer between functions.

    Also, your indentation is ........-poor.
    I had it really neat before but I was trouble shooting and moving stuff around. Anyway its working now, I appreciate the tip. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Determining words in a file by reading individual characters.
    By HellaWhiteDude in forum C++ Programming
    Replies: 0
    Last Post: 10-14-2011, 02:10 PM
  2. Replies: 1
    Last Post: 04-27-2011, 10:56 PM
  3. Reading words and analyzing words from file
    By desmond5 in forum C Programming
    Replies: 7
    Last Post: 02-26-2008, 03:51 PM
  4. Replies: 5
    Last Post: 04-26-2007, 08:50 AM
  5. Reading a File and returning single words
    By kapri in forum C++ Programming
    Replies: 5
    Last Post: 05-11-2005, 05:13 AM