-2

'too few argument in function call' (visual studio). i cant open the file. How to solve this?

#include <stdio.h>

int main() {
    FILE * fp;
    fp = fopen_s("testfile.txt", "w"); //to open file
    return 0;
}
  • 1
    So the first thing you need to do is work out the difference between C#, C and C++, because you are obviously confused... Note, you used the C# tag (which in turn brings a bunch of C# professionals and enthusiasts to eagerly visit a question full of C# goodness, only to leave bitterly disappointed) :) – TheGeneral Sep 16 '21 at 08:27
  • 3
    Change `fopen_s` to `fopen`. – Tom Karzes Sep 16 '21 at 08:34
  • Have a look: https://stackoverflow.com/questions/2575116/fopen-fopen-s-and-writing-to-files – Mike Sep 16 '21 at 08:41
  • visual studio should use 'fopen_s' –  Sep 16 '21 at 08:42

1 Answers1

1

Too few arguments in function call means that you called a function (fopen_s in this case) with 2 arguments, but the function requires 3 arguments.

How to solve? Simply search and read the documentation for fopen_s(). Often enough, there are also examples of usage.

After reading the docs, you will find out that this is how you use fopen_s():

int main(void) {
  FILE* fp = NULL;

  if (fopen_s(&fp, "testfile.txt", "w") != 0 || fp == NULL) {
    printf("ERROR: cannot open file\n");
    return EXIT_FAILURE;
  }

  ...
}
Luca Polito
  • 2,387
  • 14
  • 20