-2

I am trying to create a program which :

1. counts the number of the words

2. counts the length of the words from a file but

My problem is that I notice something that I did not expected the file has four words words but when it counts the length of words the program prints a random number 77

The file :

vasilis
giorgos
anastasia
nikos

code :

#include<stdlib.h>
#include<string.h>

int main ()
{
    FILE *fp ;

    fp=fopen("C:\\Users\\TOSHIBA\\Downloads\\sample.txt","r");

    if (fp==NULL)
    {
        printf("the file is empty");
    }
    //count the number of words
    int counter =1;
    char ch;
    while((ch=fgetc(fp)) != EOF)
    {
       if (ch=='\n')
       {
           ++counter;
       }
    }

    fseek(fp,0,SEEK_SET);

    //count the length of words

    int length[counter];
    char ch1;
    int counter1 = 0 , i=0;

    while( (ch1 = getc(fp))!= EOF )
    {
        if (ch1 == '\n')
        {
            length[i]=counter1;
            i++;
            counter1=0;
            continue;
        }
        counter1++;

    }

    // print the length of every word

    for (i = 0; i < counter; i++)
    {
        printf("\n%d",length[i]);

    }
    return 0;
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
vg97
  • 1
  • 2
  • "but when it counts the length of words the program prints a random number" -- Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine in which line your program stops behaving as intended? If you did not try this, then you may want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Andreas Wenzel May 29 '22 at 14:01
  • Unless there is a newline at the end of your file, the last line's length will never be initialized. – Alexander Guyer May 29 '22 at 14:03
  • @AlexanderGuyer Ok I understand what you say but how can I avoid this if I want to count the length of the words? When I tried `\0` instead of `\n` it prints more random values – vg97 May 29 '22 at 14:07
  • I encourage you to think carefully about the conditions upon which you'd like to record the length of a line. It seems that you want to do it at the end of the each line. How do you know when a line ends? A line ends whenever there is a new line character. However, a line may *also* end when the file ends; this is an edge case. Think about how to express this logic in code. – Alexander Guyer May 29 '22 at 14:13
  • Additionally, I should note that line ending encodings vary between operating systems. So manually discovering the end of a line is not a very robust solution in general; you should look into standard solutions for reading an entire line (they'll be implemented appropriately in each OS's implementation of the standard library). [Here's](https://stackoverflow.com/questions/41861208/how-to-read-one-whole-line-from-text-file-using) a relevant SO question. – Alexander Guyer May 29 '22 at 14:16
  • @AlexanderGuyer Thanks for your answer and I know it is better to use `fgets()` but in the format of this function has to declare how many characters I want to read from the file . I was trying to create a program which I count the length of the words in case that I did not know the line length – vg97 May 29 '22 at 14:29

1 Answers1

0

The program cannot read the number of characters in the last word as there is no \n at the end of that word.

The code should be modified like this:

    while( (ch1 = getc(fp))!= EOF )
    {
        if (ch1 == '\n')
        {
            length[i]=counter1;
            i++;
            counter1=0;
            continue;
        }
        counter1++;
    }
    length[i] = counter1; /* handle un-terminated last line */
Useless
  • 64,155
  • 6
  • 88
  • 132
Bad Liar
  • 16
  • 3