Thread: passing a filename as a parameter..

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    27

    Question passing a filename as a parameter..

    How can a filename be passed as a parameter from one function to another function? For instance I have a filname as a string defined as:
    char[] = "filename.txt" ;
    What would the format of the function that passes it look like and the format of the receiving function look like?
    Thanks!
    Flight

  2. #2
    Wannabe Coding God
    Join Date
    Mar 2003
    Posts
    259
    you pass the adress to the first element in the string.
    They say that if you play a Windows Install CD backwords, you hear satanic messages. That's nothing; play it forward and it installs Windows.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >How can a filename be passed as a parameter from one function to another function?

    You could do something like this.
    Code:
    #include <stdio.h>
    
    void foo(const char *filename)
    {
       FILE *file = fopen(filename, "r");
       if(file != NULL)
       {
          printf("%s: opened\n", filename);
          fclose(file);
          printf("%s: closed\n", filename);
       }
       else
       {
          perror(filename);
       }
    }
    int main(void)
    {
       const char filename[] = "filename.txt";
       foo(filename);
       return 0;
    }
    
    /* my output
    filename.txt: No such file or directory
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 12:13 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. typecasting data for parameter passing
    By daluu in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2004, 02:58 PM