
-----------------------------------
deltatux
Wed Jan 28, 2009 9:46 pm

Opening files with as a function?
-----------------------------------
Alright,

This is confusing. The specification laid out by my prof is:


a function that receives the address of a C-style, null-terminated string containing the name of the file that holds the prefix table
 FILE* open(const char filename

However, usually this is how I open files:

void readFile (){
FILE* fp = NULL;
fp = fopen("file.txt", "r");
}


So how do I adopt this?

deltatux

-----------------------------------
OneOffDriveByPoster
Wed Jan 28, 2009 11:01 pm

Re: Opening files with as a function?
-----------------------------------
Did your prof say that function was available, or did he say you had to write one?

-----------------------------------
deltatux
Thu Jan 29, 2009 1:29 am

Re: Opening files with as a function?
-----------------------------------
I have a feeling that I have to write one. However, I don't understand how you can use:


FILE* open (const char filename[]){

}


to open a file because the way I understand opening files is that it's a variable that acts as a boolean when you open/close files.


void readFile (){ 
FILE* fp = NULL; 
fp = fopen("file.txt", "r"); 
} 


which the variable is WITHIN a function and not a function itself. So, I'm not too sure how this is going to work. I was wondering if anyone knows how this works or can point me to resources.

Many thanks,
deltatux

-----------------------------------
OneOffDriveByPoster
Thu Jan 29, 2009 5:00 pm

Re: Opening files with as a function?
-----------------------------------
Information about an open file is stored in a FILE structure/type.

`FILE *' is a pointer to a FILE structure.  You can pass this safely to other functions
or even out of the function where you opened the file.

You can expect that the FILE structure pointed to by the FILE * from fopen() is not in automatic/stack storage.

-----------------------------------
deltatux
Thu Jan 29, 2009 10:33 pm

RE:Opening files with as a function?
-----------------------------------
so like this?


FILE* open (const char filename[]){
      fopen(filename, "r");
}

int main (){
   fread = open(filename);
   if (fread !NULL) {
      cout 