Please Help If You Could...

This is a discussion on Please Help If You Could... within the C Programming forums, part of the General Programming Boards category; Hello Everyone, I was wondering if someone could please help me out. I am a newbie to the world of ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    17

    Question Please Help If You Could...

    Hello Everyone,

    I was wondering if someone could please help me out. I am a newbie
    to the world of "C" and I have been having some difficulty with
    something I have been trying to do. I am just starting to learn
    about pointers and passing to/from functions.

    What I am trying to accomplish is the following.

    I have a function that reads input from the user and asks for a file
    path and name. I then check to see if it exists and if so I return
    the FILE pointer. Everything is working just fine. However I would
    like to also be able to return to my main() function the path that
    the user entered. I have used a char array for the path_name input
    and have not figured out a way to pass that back using pointers to
    main().

    Any suggestions would be helpful.

    Thanks in advance!

    - shane

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Code:
    FILE * myfunction(char *path)    // pass a pointer as paramater
    {
      ...
      fgets(path, 499, stdin);             // fill passed pointer 
      path[strlen(path)-1] = '\0';        // with the string
      ...
      return filepointer;
    }
    
    ...
    
    int main(void)
    {
      ...
      char path[500];
      FILE *mega = myfunction(path); // path is now the path entered.
      ...
    }
    ;

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Use salem's code, he's a professional. And I didn't pass the size of the array. Naughty.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    17

    Wink Thank You Salem and Brian

    Thank you so much for your help and code examples, I have incorporated them and everything is working great!

    - shane

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21