R
R
rsatarov2017-10-14 18:55:18
C++ / C#
rsatarov, 2017-10-14 18:55:18

What is the error in reading text from a file (C)?

Actually, code.

open me
#include <stdio.h>

char * getStringByPath(char *filePath);

int main() {
  char *filePath = "D:/test/input.txt";

  char *result = getStringByPath(filePath);

  return 0;
}
char *getStringByPath(char *filePath) {
  FILE *inputFile;
  char *resultString = NULL;

  errno_t err = fopen_s(&inputFile, filePath, "r");
  if (!err) {
    fscanf_s(inputFile, "%s", resultString, __crt_countof(resultString));
  }

  return resultString;
}


When reading from a file, this error is obtained.
Vo9DbDaq2nE.jpg
In debugging, it showed that the problem is in the line with fscanf_s. But just what is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-10-14
@rsatarov

char *resultString = NULL;
You are reading into an invalid buffer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question