working with file name [] ???

This is a discussion on working with file name [] ??? within the C++ Programming forums, part of the General Programming Boards category; beginner confused here: Code: FILE* functionName(char []) where i pass a string as the file name, open the file and ...

  1. #1
    rox
    rox is offline
    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,796
    >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
    rox
    rox is offline
    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,796
    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
    rox
    rox is offline
    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, 05:27 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Need help on understandind arrays
    By C++mastawannabe in forum C++ Programming
    Replies: 9
    Last Post: 06-16-2007, 11:50 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

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