Thread: getting filename from a file pointer

  1. #1
    Unregistered
    Guest

    getting filename from a file pointer

    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?

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    I'm sort of confused on what exactly you want, but...
    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;
    }
    Does this help any?
    That was off the top of my head.
    The world is waiting. I must leave you now.

  3. #3
    Unregistered
    Guest
    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.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Unregistered
    I can just print out the file pointer as a string.
    errr.... no. You can't do this:
    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);
    }
    You mind expanding on your comments please?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    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.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Was it something like this maybe?
    I suppose so, but that isn't what the original poster asked for:
    what function could i call on the pointer to give me that filename
    I don't know of one that can do what is being asked.....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > 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.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    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]

  9. #9
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    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.

  10. #10
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    do you mean using the argv as a string in the first parameter of fopen()?
    think only with code.
    write only with source.

  11. #11
    Unregistered
    Guest
    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);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM