Author |
Message |
deltatux
![](http://compsci.ca/v3/uploads/user_avatars/2510662304931ac0b5cb22.png)
|
Posted: Wed Jan 28, 2009 9:46 pm Post subject: Opening files with as a function? |
|
|
Alright,
This is confusing. The specification laid out by my prof is:
Quote:
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[]);
This function opens the named file for read access and returns the address of the file pointer, if successful; NULL otherwise.
However, usually this is how I open files:
code: |
void readFile (){
FILE* fp = NULL;
fp = fopen("file.txt", "r");
}
|
So how do I adopt this?
deltatux |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Wed Jan 28, 2009 11:01 pm Post subject: Re: Opening files with as a function? |
|
|
Did your prof say that function was available, or did he say you had to write one? |
|
|
|
|
![](images/spacer.gif) |
deltatux
![](http://compsci.ca/v3/uploads/user_avatars/2510662304931ac0b5cb22.png)
|
Posted: Thu Jan 29, 2009 1:29 am Post subject: 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:
code: |
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.
code: |
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 |
|
|
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Thu Jan 29, 2009 5:00 pm Post subject: 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. |
|
|
|
|
![](images/spacer.gif) |
deltatux
![](http://compsci.ca/v3/uploads/user_avatars/2510662304931ac0b5cb22.png)
|
Posted: Thu Jan 29, 2009 10:33 pm Post subject: RE:Opening files with as a function? |
|
|
so like this?
code: |
FILE* open (const char filename[]){
fopen(filename, "r");
}
int main (){
fread = open(filename);
if (fread !NULL) {
cout << "open successfully" << endl;
} else {
cout << "can't open file" << endl;
}
return 0;
}
|
deltatux |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Fri Jan 30, 2009 12:33 am Post subject: RE:Opening files with as a function? |
|
|
My eyes! The mix of C and C++ written incorrectly in this thread is horrible. |
|
|
|
|
![](images/spacer.gif) |
md
![](http://compsci.ca/v3/uploads/user_avatars/1849317514ed6c4399768d.png)
|
Posted: Sat Jan 31, 2009 12:12 am Post subject: RE:Opening files with as a function? |
|
|
deltatux, try compiling your code with -Wall -Wextra and see what warnings and errors you get. |
|
|
|
|
![](images/spacer.gif) |
deltatux
![](http://compsci.ca/v3/uploads/user_avatars/2510662304931ac0b5cb22.png)
|
Posted: Tue Feb 03, 2009 12:47 am Post subject: Re: RE:Opening files with as a function? |
|
|
wtd @ Fri Jan 30, 2009 12:33 am wrote: My eyes! The mix of C and C++ written incorrectly in this thread is horrible.
I fixed the code, I broke some rules on the assignment and abolished most of the C mixing, it's horrid.
I fixed it, thanks anyways guys.
deltatux |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|