0

I need to get number of characters in each line and then paste it into another file (school project) but even my prototype is already not working.

I have already trying continuing seeing whether problem resolves itself when I paste it into another file but it didn't work so I'm back to this.

int main()
{
    FILE *fw;
    FILE *fr;
    int i = 0;
    char c;
    fr = fopen("vstup.txt","r");
    fw = fopen("vystup.txt","w");
    while ((c = getc(fr)) != EOF)
    {
        while ((c = getc(fr)) != '\n')
        {
            i++;
        }
        printf("%d\n", i);
    }
}
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Punkcan
  • 3
  • 1
  • 1
    What exactly is not working for you? By the way, you are skipping the first character in each line. – goodvibration May 23 '19 at 21:42
  • 1
    You don't zero out the counter... – Mad Physicist May 23 '19 at 21:42
  • 1
    `char c` should be `int c`. – Barmar May 23 '19 at 21:43
  • Maybe have a look at [this post](https://stackoverflow.com/q/8586722/9232218) and the link in the comments. – Yastanub May 23 '19 at 21:43
  • 1
    Possible duplicate of [Comparing unsigned char and EOF](https://stackoverflow.com/questions/8586722/comparing-unsigned-char-and-eof) – Yastanub May 23 '19 at 21:44
  • @Yastanub That's the least of the problems. – Barmar May 23 '19 at 21:51
  • the posted code does not compile! When asking a question about a run time problem, as this question is doing, Please post a [mcve] so we can reproduce the problem and help you debug it. – user3629249 May 24 '19 at 00:24
  • At a minimum, the posted code is missing the needed `#include` statements for the needed header files. Are you expecting us to guess as to what header files your code is actually including? – user3629249 May 24 '19 at 00:27
  • OT: when calling `fopen()`, always check (!=NULL) the returned value to assure the operation was successful. If not successful, call `perror( "my error message" )` to output both your error message AND the text reason the system thinks the problem occurred to `stderr`. – user3629249 May 24 '19 at 00:29
  • Please read/understand the MAN page for the functions that you use. For instance, the function: `getc()` actually returns a `int`, not a `char` and EOF is a `int` – user3629249 May 24 '19 at 00:30
  • the posted code, at best, counts the number of characters (except the newline sequences) in a file and displays that count – user3629249 May 24 '19 at 00:32

2 Answers2

2

c has to be declared int, not char. EOF might not be a valid char value, so the inequality might succeed forever.

You shouldn't call getc() twice. You never check the result of the first call to see if it's a newline.

You need to set i back to 0 after you read a newline, otherwise you'll keep adding the next line's length to the previous line.

You should only print the length when you read a newline, not every time through the loop. And if the last line doesn't end with newline, you should print its length after the loop; you can tell this happened if i is not 0 when it's done.

Since the lengths should be written into the other file, you should use fprintf().

while ((c=getc(fr)) != EOF) {
    if (c == '\n') {
        printf("%d\n", i);
        i = 0;
    } else {
        i++;
    }
}
if (i != 0) {
    fprintf(fw, "%d\n", i);
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

You don't need a nested loop. You need to zero out the counter either way, so you may as well just use an if instead of a while:

int main()
{
    FILE *fw, *fr;
    int count = 0;
    int c;

    fr = fopen("vstup.txt","r");
    fw = fopen("vystup.txt","w");

    while ((c = getc(fr)) != EOF) {
        count++;
        if(c == '\n') {
            fprintf(fw, "%d\n", count);
            count = 0;
        }
    }

    fclose(fr);
    fclose(fw);
}

This version will count newlines as valid characters.

Note that c must be an int, not a char, so that it can hold every character value returned by getc as well as EOF, which is not a character value.

And don't forget to close your files!

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264