0
#include <stdio.h>
#include <stdlib.h>

#define SIZE 200

int main() {

    FILE *input = fopen("word_list_final.txt", "r");
    char buffer[SIZE];
    int counter = 0;

    if (input == NULL) {
        printf("Error! Could not open file\n"); 
        exit(-1);
    }

    while (fscanf(input, "%s\n", buffer) != EOF) {
        counter++;
    }

    fclose(input);
    printf("%d\n", counter);
    return 0;
}

After executing, program prints correct result and mentioned message. (File, I'm reading from, contains one word per line)

Outputs:

89937042 *** stack smashing detected ***: terminated Aborted (core dumped)

How to get rid of error message?

  • 2
    Does the bug disappear if you change `while (fscanf(input, "%s\n", buffer) != EOF) {` to `while ( fscanf(input, "%199s", buffer) == 1 ) {` ? – Andreas Wenzel Jan 04 '22 at 01:09
  • Questions seeking debugging help should generally include a [mre]. Your code itself seems to fulfill this criteria, but you have not provided a sample input file that reproduces the bug. – Andreas Wenzel Jan 04 '22 at 01:14
  • My only explanation for this bug is that you are overflowing `buffer`, which would only happen if you have a word longer than 199 characters. I am unable to reproduce this bug when I create my own input file, and you have not yet provided any input file which claims to reproduce the bug. – Andreas Wenzel Jan 04 '22 at 01:28
  • @AndreasWenzel now bug disappeared, but I got result 89937045, 3 words more then file contains, and a do not have any word longer then 199 chatacters – Luka Illich Jan 04 '22 at 01:30
  • 1
    Please include the 2-3 longest lines in `word_list_final.txt` in the question – Ted Lyngmo Jan 04 '22 at 01:33
  • 1
    Between the line containing the `fscanf` and `counter++`, I suggest that you add the following line (inside the `while` loop): `if ( strlen(buffer) > 198 ) {printf( "input too long\n" ); exit( EXIT_FAILURE );}` You will have to add `#include ` for this to work. When you run your program afterwards, do you get the error message `"input too long"`? – Andreas Wenzel Jan 04 '22 at 01:37
  • Thanks for your help, actually there were words longer than 200 characters. I apologize for my self-confidence. – Luka Illich Jan 04 '22 at 01:47
  • @LukaIllich: Then that is probably the reason why you count 3 more words than are actually in the file. Since `scanf` can only match 199 characters at once, you will need two or more loop iterations to handle a word with more than 199 characters. – Andreas Wenzel Jan 04 '22 at 01:50
  • `fscanf` does not return `EOF`. Use: `== 1` in the `while` – Craig Estey Jan 04 '22 at 01:51
  • @CraigEstey: `fscanf` can return `EOF`. However, you are right that `== 1` is better, because it can also return `0`. – Andreas Wenzel Jan 04 '22 at 01:52
  • Even if you have now solved your problem, if you want to prevent your question from being closed, you may still want to provide a sample input file which reproduces the problem. That way, your question would also have a higher chance of being upvoted. – Andreas Wenzel Jan 04 '22 at 02:04
  • The `'\n'` in `"%s\n"` is superfluous. Get rid of it. – David C. Rankin Jan 04 '22 at 06:12

1 Answers1

1

Most likely your input file contains a line of 200 characters or more, so when fscanf places the bytes in buffer, there is an overflow, which overwrites whatever is on your stack after the buffer array.

Sam
  • 109
  • 5