C file creation error

Asked

Viewed 91 times

3

I am trying to create a C file but it is not working. The operating system is Linux.

#include <stdio.h>

void main (FILE *file) { 
    file = fopen("/home/Documentos/teste.odt", "w");
    fprintf(file, "alo\n"); 
    fclose(file);
}
  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

6

Do this:

#include <stdio.h>

void main() {
    FILE *file = fopen("/home/Documentos/teste.odt", "w");
    fprintf(file, "alo\n"); 
    fclose(file);
}

I put in the Github for future reference.

You can’t get one FILE as a parameter in main(). the function main() can only receive strings (pointers to char). this does not mean that a function cannot receive a FILE. Of course you can, but if you are going to receive this type, you need to receive a data with this information. In other words, you have to create a FILE somewhere and pass this structure created to the function that is prepared to receive this type of information. The problem of main() is that its parameters arrive through the operating system (directly or indirectly) and in the OS it is not possible to create the type FILE and pass, he only knows how to pass strings.

  • Thank you! It worked!

  • You can accept an answer when it solved your posted problem. In addition to being able to vote for all the answers you think helped in any way. See how in [tour].

Browser other questions tagged

You are not signed in. Login or sign up in order to post.