Thread: Opening File in C

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    You really need to read the lesson on functions. A couple of notes:
    1. Do not cast the return of malloc. Read FAQ-Casting Malloc
    2. You free your buffer before you use it. That data is gone.
    3. Your buffer is local to your function. Once you leave that function the buffer goes out of scope and you can't access it anymore (if you didn't free it).
    So, what to do:

    1. Place your buffer in main. (char* buffer=NULL)
    2. Pass your buffer to your open file function
    3. Open your file
    4. alloc space for your buffer. Check to ensure malloc worked (if(!buffer) exit(1))
    5. Fill your buffer with fread.
    6. close the file

    6A... return from the OpenFile function.


    7. Call your decode function and pass your buffer to it.

    8. When done decoding and displaying, free your buffer and exit.

  2. #17
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by CommonTater View Post
    So, what to do:

    1. Place your buffer in main. (char* buffer=NULL)
    2. Pass your buffer to your open file function
    3. Open your file
    4. alloc space for your buffer. Check to ensure malloc worked (if(!buffer) exit(1))
    5. Fill your buffer with fread.
    6. close the file

    6A... return from the OpenFile function.


    7. Call your decode function and pass your buffer to it.

    8. When done decoding and displaying, free your buffer and exit.
    Haha...yeah I guess I do need to be that specific for this thread. Thanks for helping out.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by AndrewHunter View Post
    Thanks for helping out.
    Whatever I can do to embarrass help a friend!
    Last edited by CommonTater; 08-19-2011 at 07:44 PM.

  4. #19
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Almost at the point of giving up on this. I have been reading the link on fread that you have posted but I am getting more confused. When you say place your buffer in main which part of this do you mean?

  5. #20
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by bodyboarder91 View Post
    Almost at the point of giving up on this. I have been reading the link on fread that you have posted but I am getting more confused. When you say place your buffer in main which part of this do you mean?
    Something like this:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void openfile(char**);
    void loadMenu2(char*);
    int main(void){
    
    	char *buffer=NULL;
    
    	//your case statements here
    	//For case1:
    	openfile(&buffer);
    	loadMenu2(buffer);
    
    	return(0);
    }
    void openfile(char **buffer){
    
    	//open file and obtain size
    	*buffer = malloc(lsize * sizeof(char));
    	if(!(*buffer)){
    		printf("Error with malloc");
    		exit(1);
    	}
    	//read your file into buffer, using *buffer
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File opening
    By cnewbie1 in forum C Programming
    Replies: 2
    Last Post: 12-28-2010, 08:16 AM
  2. About file opening
    By kira_coder in forum Windows Programming
    Replies: 5
    Last Post: 03-14-2010, 11:15 PM
  3. File opening
    By al_engler in forum C Programming
    Replies: 3
    Last Post: 12-27-2006, 05:46 PM
  4. file opening?
    By ssjnamek in forum C Programming
    Replies: 24
    Last Post: 03-01-2006, 08:15 AM
  5. opening a file in dos
    By makveli in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2003, 04:42 PM