function to open file?

This is a discussion on function to open file? within the C Programming forums, part of the General Programming Boards category; Hi everyone. I've been trying to write a function which should open files. here it is: Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    230

    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

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    230
    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, 08:51 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 03:37 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21