Can you pass FILE pointers as arguements to a function? When I do it I get segmentation error.
FILE * k;
void function(FILE *p);
Printable View
Can you pass FILE pointers as arguements to a function? When I do it I get segmentation error.
FILE * k;
void function(FILE *p);
Actually, I think it would be better to return whatever fp no matter what happens and let the user decide what to do with it.Code:FILE* OpenReadOnlyFile(const char* file) {
FILE* fp = fopen(file, "r");
return fp;
}
int main(int argc, char** argv) {
if(argc > 1) {
FILE* myFile = OpenReadOnlyFile(argv[1]);
if(myFile == NULL) {
// ...
} else {
// ...
}
}
}
darsunt: Is the file pointer valid? Did you initialize it to NULL / check the return value on fopen() ? Perhaps show some code?
MDOfRockyView: What is the point of the second argument to OpenAFile? You can't return any values through it (and you do so through the return value anyways...) And you pass garbage to that when you call it from main()...
I wrote this function to take a FILE pointer as arguement and open it to a file. The function itself runs without complaint, so the file apparently was opened, but when I try to use fgetc on the supposedly opened FILE pointer I get a segmentation fault (memory error)
//after run function try use fgets on p, get segmentation errorCode:void open_file(FILE * p)
{
char fetch[20];
printf("Enter name of file: ");
gets(fetch);
if(!(p=fopen(fetch,"r+t")))
{
printf("Cannot open file\n");
exit(1);
}
}
I can pass a file pointer that has already been opened to a function and use fgetpos() on it successfully
If you pass an integer to a function, how can you fill it so that it retains any alterations you've made to it? If you can answer that, you've just answered how to do the same thing with a FILE pointer, or any variable for that matter.
If you can't, try reading the post right before yours.
Quzah.
>> gets(fetch);
Often frowned upon! It's better to use getline(); gets doesn't flush he buffer. Read this guy's sig.
getline() is not a standard C function, fgets() is, and is the preferred alternative to gets(). The problem with gets() has nothing to do with "flushing the buffer", and is wholly to do with not allowing any limit on amount of characters read to be specified, thus being able to be overflowed.Quote:
Originally Posted by twomers
May. So you should check first and not just do this.Quote:
Originally Posted by MDofRockyView
Much like it says in the FAQ.Code:filename[strlen(filename) - 1] = '\0';
>> not allowing any limit on amount of characters read to be specified
Good point. Sorry about that. I haven't seen gets(); in a while, knew there was something bad about it, and and assumed it was something to do with the buffer.
...
>> getline() is not a standard C function
Also a good point. I've been doing C++ for the past long time, and I think it's standardised there, so suggested it.
No, it retains it if it is there. If it isn't there, then what is it that you are overwriting?Quote:
Originally Posted by MDofRockyView