Created a file in the project folder. I run the following code:

FILE * f; char ch; f = (FILE *) fopen("results.txt", "r"); ch = getc(f); printf("%s", ch); fclose(f); 

The program crashes with the error:

"0xC0000005: access violation when reading at 0x00000074."

Although if you open the file for recording, everything is recorded normally. I tried to run the program as an administrator - the error does not disappear.

Tell me, please, what's the matter?

  • @iproger, although there is an error in the format in printf (about wkm @KoVadim wrote to you), the result of fopen never interferes with checking. #include <sysexits.h> ... if (! (f = fopen ("results.txt", "r"))) {perror ("fopen (\" results.txt \ ", \" r \ ") "); exit (EX_DATAERR); } ... - avp
  • I check, but here I just pulled out the code, I tested it without too much code. And exit is not clear why, if you can do return 0 from main - iproger
  • one
    @iproger, of course, from main possible and return EX_DATAERR; Why not -1, but EX_DATAERR? Because it is an error in the data transmitted by the user. See sysexits.h for the sysexits.h program return codes (generally accepted at least in * nix), see sysexits.h . - avp
  • @avp, I see, thanks. Just connect the library, it is a pity to load the program) We are taught in the university to code on the SI without too much, the teacher leans on each byte. I don’t really like it so much; on the contrary, I want the code to be understandable so that it is convenient: for more functions, structures (instead of several variables) and so on. Therefore, I will still use this library) - iproger
  • @iproger, the exit function (along with fopen, printf and a huge number of others) is usually found in libc , which in almost all operating systems is shared (.so or .dll) and its code is not included directly in the boot module. The only instance loaded into memory is used by all programs that are currently running. Of course, a few bytes of the "adapter" linker will add, but I do not think that this is critical. - avp

1 answer 1

The problem is not in reading the file. Line problem

 printf("%s", ch); 

the fact is that ch is a character (char), and printf will print a string ( %s parameter). And what is a string? it's just a sequence of characters up to the first zero. Here it is, starting from the address of a local variable, runs through memory to the first zero character.

  • Thank you very much, which is why a normal debugger was not done, which would indicate to me that "Perhaps I did not correctly pass the parameter% s". Just somehow postponed s, d, lf, f. About c and forgot completely. Thank! - iproger
  • 2
    judging from the fact that the error sounds like "0xC0000005", you use the Microsoft compiler. And he is. But gcc, if you turn on the normal warning level (at least -Wall ), at the compilation stage will be criticized: warning: format '% s' expects an argument of type 'char *', but argument 2 has type 'int' [-Wformat =] printf ("% s", ch); I highly recommend including warnings! - KoVadim
  • Yes, I use mykrasoft (I have to try gcc, thanks - iproger