0

I'm trying to write a program that i have to enter a word from the keyboard and then it will check how much times appears in a text file using the strcmp() function. here is my code. i can write the word but when i enter the enter button the program stops. anyone can help me to figured out whats going wrong?

#include "stdafx.h"
#include <string.h>
#include <stdio.h>


int main()
{
char input[20];
char string[20];
int num = 0;


FILE *text;

printf("Enter a word:\n");
scanf_s("%s\n", &input);

fopen_s(&text, "C:\\Users\\USER\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication8\\text.txt", "r");

if (text == NULL) {
    printf("Failed to open file\n");
    return (-1);
}

while (!feof(text))
{
    fscanf_s(text, "%s", string);
    if (!strcmp(string, input));
    num++;
}

printf("we found the word %d times\n", num);

return 0;
}`
alk
  • 66,653
  • 10
  • 83
  • 219
John Smith
  • 19
  • 5

2 Answers2

1

[From this "C:\\Users\\USER\\Documents\\Visual Studio 2015\\ ... I concluded the compiler in use is MS-VC]

Besides the wrong use of feof() as pointed out by MarianD in this answer, there is the following fatal mistake:

This line misses the size of the buffer to scan into:

  fscanf_s(text, "%s", string);

It should be

  fscanf_s(text, "%s", string, (unsigned) sizeof string);

Same plus 1 issue here:

  scanf_s("%s\n", &input)
  1. Pass input, not its address. %s expects a char* (to which input decays). Doing &input would in fact pass the same value, but use the wrong type, namely char(*)[20], which would invoke UB.
  2. Pass its size:

    scanf_s("%s\n", input, (unsigned) sizeof input)
    

From fscanf_s documentation:

The main difference between the more secure functions (that have the _s suffix) and the other versions is that the more secure functions require the size in characters of each c, C, s, S, and [ type field to be passed as an argument immediately following the variable.

[...]

The size parameter is of type unsigned, not size_t.

Community
  • 1
  • 1
alk
  • 66,653
  • 10
  • 83
  • 219
0

change

scanf_s("%s\n", &input); 

by

scanf_s("%s\n", input);

Indeed input is already a char* and you are sending a char (*)[20].

alk
  • 66,653
  • 10
  • 83
  • 219
Glick
  • 121
  • 7
  • **No, you are wrong** (in spite the idea behind it is in general true). `input` and `&input` are in this case the same - see [How come an array's address is equal to its value in C?](http://stackoverflow.com/a/2528328/7023590) – MarianD Nov 06 '16 at 14:30
  • Hmm, maybe that's because you are not compiling with the same flags as me but if you try with -pedantic -Wall flag you'll get a warning. – Glick Nov 06 '16 at 14:43
  • It is only **warning** (incompatible pointer type) as compiler don't know anything about **using** this in the body of the called function (particularly if it will perform *pointer arithmetic* - which **is** different for `input` and `&input`). **Values** (= addresses) are the same, **behavior** is **in this case** the same, too, so **your answer has no chance to solve** the problem. (**I'am sorry** that you are not correct but is it not pleasurable to learn something new?) – MarianD Nov 06 '16 at 15:42
  • 2
    @MarianD: Same value but wrong type, so using `&input` invokes UB by passing the wrong pointer type, namely `char(*)[20]` instead of `char*`. – alk Nov 06 '16 at 16:42
  • 1
    Assuming MS-VC, this `scanf_s("%s\n", input);` is incomplete, to not say wrong. – alk Nov 06 '16 at 16:59
  • I agree with you (2 times) but my point was that in this case it doesn't make a difference of behavior (and so it doesn't help the person who asked this question). – MarianD Nov 06 '16 at 17:03