Thread: have to prompt the user for file and then count words, characters and lines.

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    have to prompt the user for file and then count words, characters and lines.

    I can get the prompt to ask for the file, but nothing after that. Please help. Also, I know the fflush i bad form, but it's what the professor asked us to use.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
        FILE *inFile;
        char filename[64];    
        
        printf("\nEnter a file name: ");
        gets(filename);
        fflush(stdin);
        getchar(); 
        
        inFile = fopen(filename, "r");
        if(inFile == NULL)
        {
                  printf("\nThe File %s was not successfully opened.", filename);
                  printf("\nPlease check that the file currently exists. \n");
        return(1);
    }
        
        int countword = 0;
        int countchar = 0;
        int linecount = 0;
        int count = 0;
        char c;
        
        c = getc(inFile);
        while (c != EOF );
    {
        countchar++;
        if (c == '\n') linecount++;
        if (c == ' ') countword++;
        c = getc(inFile);
    }
        fclose (inFile);
        if (countchar != 0); 
        {
        printf("Number of characters = %d, number of lines = %d, Number of words%d\n", countchar, linecount, countword);
    }
        fflush(stdin);
        getchar();

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    SourceForge.net: cpwiki
    Follow the links on fflush(stdin) and gets()
    The one on indentation would be worth a read as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > Also, I know the fflush i bad form, but it's what the professor asked us to use.
    Then he's an idiot. Show him this link.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    12
    ok, but that's not why the program won't work. Does anyone know what I'm doing wrong?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well having formatted it to resolve the crap indentation (if you did this, you might actually see something), we see
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      FILE *inFile;
      char filename[64];
    
      printf("\nEnter a file name: ");
      gets(filename);
      fflush(stdin);
      getchar();
    
      inFile = fopen(filename, "r");
      if (inFile == NULL) {
        printf("\nThe File %s was not successfully opened.", filename);
        printf("\nPlease check that the file currently exists. \n");
        return (1);
      }
    
      int countword = 0;
      int countchar = 0;
      int linecount = 0;
      int count = 0;
      char c;
    
      c = getc(inFile);
      while (c != EOF);
      {
        countchar++;
        if (c == '\n')
          linecount++;
        if (c == ' ')
          countword++;
        c = getc(inFile);
      }
      fclose(inFile);
      if (countchar != 0);
      {
        printf("Number of characters = %d, number of lines = %d, Number of words%d\n", countchar,
               linecount, countword);
      }
      fflush(stdin);
      getchar();
    } //!! yes, this was missing
    Now, compiling the code with a real compiler (and not TurbidCrap so beloved by useless teachers)
    $ gcc -W -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:38: warning: suggest braces around empty body in an ‘if’ statement
    foo.c:24: warning: unused variable ‘count’
    /tmp/ccPYkjcg.o: In function `main':
    foo.c:(.text+0x32): warning: the `gets' function is dangerous and should not be used.

    Now, look REALLY carefully at that "empty body" warning.
    Because your while loop suffers from the same problem.

    I should also add that since mixing declarations and statements is a NEW C feature, you must be compiling this code with a C++ compiler. This is just another example of your so-called teacher teaching the compiler, and not the language. Listen, when you move to another compiler (anywhere outside of college for example), you're in for a lot of surprises.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Characters, words and lines counting
    By Darkobra in forum C Programming
    Replies: 4
    Last Post: 01-09-2012, 07:13 AM
  2. Count lines in file
    By Xpload in forum C Programming
    Replies: 12
    Last Post: 12-21-2009, 06:55 AM
  3. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  4. how do I count the lines in a text file?
    By slow brain in forum C Programming
    Replies: 4
    Last Post: 03-10-2003, 02:56 PM
  5. Replies: 2
    Last Post: 05-05-2002, 01:38 PM