Wordcounting program, problem with filereading.

This is a discussion on Wordcounting program, problem with filereading. within the C Programming forums, part of the General Programming Boards category; Hello i am trying to make a simple program that counts the words in a given .txt file, the .txt ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    5

    Wordcounting program, problem with filereading.

    Hello i am trying to make a simple program that counts the words in a given .txt file, the .txt file will be included when running the program like: ./a.out < test.txt

    I am very new to programming and this is my first program using another file, so any help would be greatly appreciated

    The wordcounting part of the program is only going to count the blank spaces between the words and should be alright, but the reading of the file is were i think the problem is.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
       int c; 
       int i;
       
       FILE* fopen(const char* argc,const char* "r"); /* openign the file */
       if (fopen==0) /* error check */
          {
          printf("Could not open file!\n");
          exit(0);
          }
       
       while (argv[c] != EOF) /* word counting part, the part i think is ok */
             {
             if (argv[c]==' ')
             {
             i++;
             c++;
             }
             else
             c++;
             }
       fclose(argc);/* close file */
             
       printf("the number of words in the file are %i",i+1);/* print result */
       
       
       system("pause");/* to get the program to stay open in the program im using  (bloodshed dev-c++) */
       return 0;
    am i using the correct way to opening the file? right now its not even compiling. I kinda messed that up trying to fix it, but when it did it froze when you tried to run it.
    as i said any help would be greatly appreciated!
    sincerly hemulen

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If your filename is the first on your command line, you probably want to use argv[1] to access it.

    Take another look at the syntax of fopen()...

    Something like f = fopen(argv[1],"r"); should suffice.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The stdin stream is already open, so you can re-direct it to your file:

    Compile this, and use it like so, (if you named the executable "stdinWorking.exe"), in a terminal window (not your IDE):

    stdinWorking <Your text file

    and all lines of text, will be displayed.

    Code:
    #include <stdio.h>
    
    int main() {
      int i,j, n; 
      char buff[80];
      printf("\n\n\n");
      while((fgets(buff, sizeof(buff), stdin))!=NULL) {
        printf("%s", buff);
      }
    
      printf("\n\n\t\t\t     press enter when ready");
    
      (void) getchar(); 
      return 0;
    }
    Last edited by Adak; 11-06-2010 at 09:25 AM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    thank you for the reply tater! i looked it over and your were very right, the program now compiles but it still crashes when you try to run it.
    the compiling also gives the following errormessages:
    on row 17 and 19: [Warning] comparison between pointer and integer

    row 17 and 19 are in the wordcounting part i thought was correct:S
    Code:
     
         while (argv[c] != EOF)
             {
         if (argv[c]==' ')
             {

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... going back to your original code...take a look at the value of c when you enter that loop....

    It might help if you posted an update (don't edit the original).

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    edit: made some minor progress, hope its ok with a small edit to the updated version



    ok ill have a look at that, i havnt made any major changes to the code but here is an update version.
    it now runs, still error when compiling tho and the result is pretty much fantasy numbers, but it feels like im getting closer

    it now

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
       int c; 
       int i;
       
       FILE* fp = fopen(argv[1],"r");
       if (fp==0)
          {
          printf("Could not open file!\n");
          exit(0);
          }
       
       while ((c = getc(fp)) != EOF)
             {
             if (argv[c] == ' ')
             {
             i++;
             c++;
             }
             else
             c++;
             }
       fclose(fp);
             
       printf("the number of words in the file are %i",i+1);
       
       
       system("pause");
       return 0;
    
    }
    tnx again for the help so far.
    sincerly Hemulen
    Last edited by Hemulen; 11-06-2010 at 09:32 AM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... now you need to look at the values of c and i .... LOL.

    Also you don't want to be testing the values of your input string (the filename), you want to be poking around in the data from the file... You're going to need fread() and some kind of buffer in there before you can count words.

    Question... are you wanting a command line like...

    Myprog < test.txt <-- text redirected to conin
    or
    Myprog test.txt <-- filename to be opened

    The former will feed the file to you as adak suggested.
    The latter means reading chunks of file from disk.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    edit: fixed some spelling and spaced the text out a bit

    lol all you want, im just happy to get some help

    sorry about the late edit to the updated version, posting two almost identical versions of the code just a few minutes apart felt like spamming thread, was hoping to be faster than you, but it looks like i wasnt.

    the command line is supposed to be myprog < test.txt but im using some inbuilt function in Bloodshed(is this the IDE?)
    to be honest i dont know how to compile the program using the command line in windows, just got a new computer and havnt found the time to create a linux partition yet.

    as i said in the edit, the program is now running, still "[Warning] comparison between pointer and integer" on the first "if" of the loop and the result is not correct.
    Ive looked over the values for i and c like you said, but i havnt figuerd it out yet.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok if you are using redirection in your command line you need to look at adak's code. He's given you the right way to do it.

    For the c and i problem... when you enter your loop, i is uninitialized and could contain any random value.

    For c you are examining argv ... which should be fine if you were parsing a filename, but if you are not passing in command line arguments argv[] will be null and poking around at it will cause problems.

    Another problem is that if you are using redirection on your command line, there is no argv to test... argc will be 0...

    Since I've never actually programmed with command line redirection, I think Adak is your best helper from here.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    5
    okey after a few more hours of work i acctually made the program work the way it should.
    I had to think over some thinks and rebuild almost everything.
    thx for all the help!

    Here is the final code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdbool.h>
    
    bool wrddef (const char a)
    {
         if ((a>='a' && a<= 'z') || (a>= 'A' && a<='Z'))
         return true;
         else
         return false;
    }
    
    int wrdcnt(const char test[])
    { 
       bool x=true, wrddef(const char a);
       int result=0;
       int i=0; 
           for (i; test[i] != '\0'; i++)
               {
               if (wrddef(test[i]))
               {
               if (x)
               {
               result++;
               x = false;
               }}
               else
               {
               x = true;
               }}
       return result;
    }
    
    
    int main(int argc, char* argv[])
    {
       int c=1;
       int a=1;
       
       FILE* fo = fopen(argv[1],"r");
       char test[100];
       if (argc==0)
       {
          printf("Could not open file!\n");
          system("pause");
          exit(0);
       }
       if (argc!=2)
       {
          printf("Could not open file!\n");
          system("pause");
          exit(0);
       }
       else
       {
          while ((a=getc(fo))!= EOF)
          {
          test[c]=a;
          c++;
          }
             
       int wrdcnt (const char test[]);  
       printf("the number of words in the file are %i\n ",wrdcnt(test));
       }
       
    fclose(fo);   
    system("pause");
    return 0;
    
    }

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Congrats Hermulen.

    This call,
    int wrdcnt (const char test[]);

    is usually just
    wrdcnt(test);

    the longer line of code is only needed in the actual function's first line.

    Frequently, the user will forget to add the input file on the command line. If argc is less than 2, you may want to print out a message alerting the user to use an input file command and return (terminating the program). Rather than trying to open a file that isn't there.

    This style of indentation is very hard to follow, when more complex code needs to be studied:
    Code:
               {
               if (wrddef(test[i]))
               {
               if (x)
               {
               result++;
               x = false;
               }}
               else
               {
               x = true;
               }}
    Subordinate lines of code, get another 2-5 char's worth of indentation. This one change will help you and everyone else who studies over your code, find bugs, and see the flow of the program, MUCH easier.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well done...

    Just one little suggestion...
    The best test for whether a file opened successfully or not is to test the file handle... if the open failed fo will be a null pointer. Testing for this would eliminate all of the error trapping steps before you actually begin reading the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 10:10 AM
  2. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Problem with Program not Quitting
    By Unregistered in forum Windows Programming
    Replies: 20
    Last Post: 06-11-2002, 11:06 PM

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