Thread: function to open file?

  1. #1
    Registered User
    Join Date
    Sep 2006

    function to open file?

    Hi everyone. I've been trying to write a function which should open files. here it is:
    #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...");
    	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).


  2. #2
    Registered User
    Join Date
    Sep 2006
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 02:37 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM