Skip to content

Instantly share code, notes, and snippets.

@morganp
Created June 12, 2014 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morganp/5aca106c74ae3f2f2ef8 to your computer and use it in GitHub Desktop.
Save morganp/5aca106c74ae3f2f2ef8 to your computer and use it in GitHub Desktop.
Function returns File Pointer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//static FILE *Sfile;
static int Sfile_open=0;
FILE *Open_Sfile(char *modname){
char file_name[256];
FILE *Sfile;
if (Sfile_open==0) {
strcpy(file_name,modname);
Sfile=fopen(file_name,"w");
printf("// Writing file %s\n",file_name);
Sfile_open=1;
};
return Sfile;
}
int main(int argc, char **argv) {
FILE *source_file;
source_file = Open_Sfile("example.txt");
fprintf(source_file, "Helloworld\n");
fclose(source_file);
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment