I want to create a program that writes a character string to a file and then reads that character string and outputs it to the screen.

However, use fopen_s (), fscanf_s (), and fprintf () as the function to be used.

I don't understand how to use fopen_s () and fscanf_s ().
fopen () and fscanf () could not be used for some reason.

Files should work with both patterns when and when they are not.

Please let me know the code as it can be simple.

This code will not output anything to the file or text.

Source code
int main ()
    FILE * fp;
    char fname [20] = {"fc.txt"};
    char a [25] = {'\ 0'};
    fopen_s (&fp, fname, "w +");
    fprintf (fp, ("% s", fname));
    fputc ('\ n', fp);
    fscanf_s (fp, "% s", a, sizeof (a));
    printf ("% s", a);
    fclose (fp);
    return 0;

Fscanf_s doesn't seem to work.

  • Answer # 1

    Although it has already been solved, it is impossible to understand until you read the above answers and comments. Is it possible to solve the problem by the following method?


    fscanf_s (fp,"% s" ;, a, sizeof (a));

    Before this, put the following code.

    fclose (fp);
        fopen_s (&fp, fname, "r");

    As in the comments, fseek () can be used to return to the beginning, but

  • Answer # 2

    just printf the character string just written.

    Now, VisualStudio cannot use dangerous functions such as scanf.
    You can use this if you turn off the security warning in the project properties and enter some options.

  • Answer # 3

    If you don't like it, ignore it.


    Use fopen_s (), fscanf_s (), and fprintf () for the function to be used.


    a challenge?
    As written about the popular fopen_s function


    The fopen_s function is standardized in C11 and can be used outside of MSVC, but the implementation of the fopen_s function is optional.
      Depending on the processing system, the fopen_s function does not exist.

    I don't wonder why such issues arise.

    I feel like a girl who is told that "the knife is dangerous, so don't touch the knife" becomes a housewife who ca n’t cook even if she goes to the bride. ... C is originally a language that can do anything (in some cases, the hardware burns ... experienced w) Don't teach it, just say you don't use fopen () or scanf () There is no help for it even if it seems negligent.