I was just wondering how, if in a function i don't have the filename that the file pointer is pointing to, what function could i call on the pointer to give me that filename?
I was just wondering how, if in a function i don't have the filename that the file pointer is pointing to, what function could i call on the pointer to give me that filename?
I'm sort of confused on what exactly you want, but...
Does this help any?Code:#include <stdio.h> int file_stuff(char * File); int main() { file_stuff("hmmmmmm"); return 0; } int file_stuff(char * File) { FILE * Tmp; Tmp = fopen("foo.txt", "a"); fprintf(Tmp, "Hmmmm\n"); fprintf(Tmp, File); fclose(Tmp); return 0; }
That was off the top of my head.
The world is waiting. I must leave you now.
Thanks anyways.
But nevermind, how silly of me. I can just print out the file pointer as a string. I thought i needed a function to get the filename (the file pointer is global, and in one function i don't get passed a filename, so i wanted to know which file was being pointed to at the moment). nevermind.
errr.... no. You can't do this:Originally posted by Unregistered
I can just print out the file pointer as a string.
You mind expanding on your comments please?Code:#include <stdio.h> int main(void) { FILE *fp; if ((fopen("junk1.c", "r")) == NULL) { perror("junk1.c"); return (1); } /* this will NOT work */ printf ("Filename: %s\n", fp); return (0); }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Was it something like this maybe?
Code:#include <stdio.h> int main() { char * FileName = "foo.txt"; FILE * Point; Point = fopen(FileName, "a"); fprintf(Point, "blah"); printf("%s", FileName); fclose(Point); return 0; }
The world is waiting. I must leave you now.
>Was it something like this maybe?
I suppose so, but that isn't what the original poster asked for:
I don't know of one that can do what is being asked.....what function could i call on the pointer to give me that filename
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
> I don't know of one that can do what is being asked.....
Maybe you have to say the magic word.
PLEASE.
I can see your point.
Printf is a function.
System is a function.
You can create your own functions. - this doesn't apply though
That's an awfully general question.
Duely noted.
The world is waiting. I must leave you now.
Shadow, you've lost me ! Are we talking about the same thing?!!
Read the original post.... it is a request for a function to obtain the name of the file opened by any given file pointer.
So, if we did this in main():
>FILE *fp = fopen ("test.txt", "r");
Then passed fp to another function like this:
>myfunc(fp);
How would myfunc know the name of the file was test.txt? This is what was asked for:
> if in a function i don't have the filename that the file pointer is pointing to, what function could i call on the pointer to give me that filename?
Now, a function doesn't exist (I don't think) that will do that job. No amount of "please" will get you one! And there's nothing general about that question, it's pretty darn specific
Last edited by Hammer; 05-16-2002 at 06:23 PM.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
You're right hammer. I was off base on that one.
Sorry - there's just been too many confusing posts today.
The world is waiting. I must leave you now.
do you mean using the argv as a string in the first parameter of fopen()?
think only with code.
write only with source.
No, Hammer was completely right. Sorry, I guess I should've posted more code instead of just asking.
Yes, the function has no clue what the filename is that main fopen'd.
So, within the function (which was passed the file pointer fp), i used:
char * fileOpen;
sprintf(fileOpen, "%s", fp);