Search code examples
cio

when trying to write file fprintf does not work


I am just trying to open and write something in file but when visual studio tries to execute fprintf program crashes here is my code

#include<stdlib.h>
int main()
{
    FILE* fPointer;
    fPointer = fopen_s(&fPointer,"‪C:\\asd.txt","w");
    fprintf(fPointer, "If ı can read this then ı can create for loop");
    fclose(fPointer);
    return 0;
}

here is the error message : Access violation writing location 0x0000003A.


Solution

  • fopen_s return value is an error number and you overwrite your file pointer with that which you should not do.