Thread: working with file name [] ???

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    34

    working with file name [] ???

    beginner confused here:

    Code:
    FILE* functionName(char [])
    where i pass a string as the file name, open the file and return fp.

    how do i work with the passed string? Should i have a name[] string so i have a name to work with or how does this work? i've never seen this before.

    Thank for the help !!!

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    What you can do is something like this
    FILE* functionName(char* name) or
    FILE* functionName(char name[])

    If your not modifying the string you can do
    FILE* functionName(const char* name)

    What happens is that the array of characters containing the filename decays into a char pointer, which points to the beginning of the array, when passing it through function.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >how do i work with the passed string?
    The same way you normally do. Since C++ passes arrays as a pointer to the first element, you have a pointer to work with. However, the type of the pointer is not const, so you can assume that it's modifiable. Though I don't see you needing to change it in this function:
    Code:
    FILE *open_reading ( char filename[] )
    {
      FILE *rin = fopen ( filename, "r" );
    
      if ( rin == NULL )
        // Do something intelligent
    
      return rin;
    }
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    Thank, so my suspicion was right.

    Problem was, i got some function definition given, and until now i was using them the way they were given. Now they only gave me :

    Code:
    FILE* fuctionName(char [])
    and i wasn't sure how to reference to my passed string.



    Code:
    FILE* functionName(char name[])
    this sound like what i wanted. Thanks.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You were given a prototype. The name of a parameter is optional in prototypes, hence the funny "char []" syntax. That is simply the parameter with no name.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    34
    great, thank Prelude. Now it makes sense.

    Couldn't they put that info in the book too? grr.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Need help on understandind arrays
    By C++mastawannabe in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2007, 10:50 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM