-
function to open file?
Hi everyone. I've been trying to write a function which should open files. here it is:
Code:
#include <stdio.h>
#include <string.h>
int openfile(char filename[], FILE **file);
int main( int argc, const char* argv[] )
{
int i=1;
FILE *file;
char filename[200];
strcpy(filename, argv[1]);
printf("file to be created: %s", argv[1]);
openfile(filename, &file);
fprintf(file, "Testing2...");
getchar();
return 0;
}
int openfile(char filename[], FILE **file)
{
file = fopen(filename, "wb");
fprintf(file, "Testing1...");
}
problem is, the file can only be accessed from inside the function. I tried to experiment a little with all the pointer operators, but I couldn't get it to work. I'm sorry if it's an easy question that's not worth answering, but I really can't get my mind to work and get it right.
By the way, my compiler does give me a few warnings (2 - one for the call and one in the function).
Thanks
-
Sorry, never mind everyone. While I was reading one of the other threads I realized my mistake. You just need to put an asterisk before "file" while you're within the function openfile. This is because you want to change the real file pointer, not the pointer pointing to the file pointer.
Sorry if my explanation wasn't that good.
Thanks again