Thread: Variable file names with fopen function?

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    7

    Variable file names with fopen function?

    Hi all,
    I was wondering if it possible to have a varying name for the filename as part of the fopen function with the filename being supplied by Command Line Argument. Off the top of my head I would say yes but that would mean that the filename would have to be in the same place everytime otherwise it wouldnt work. What are your thoughts? My thoughts are that it woul look something like this;

    Assuming the filename is in the last position

    char filename=(argc-1);

    <intermittant code>

    FILE *fopen(const char *filename, const char *r);

    Would this work? Just curious because I may have to implement it

    Alex

  2. #2
    Eager young mind
    Join Date
    Jun 2006
    Posts
    342
    yeah ,its definitely possible..

    U can think of doing something like :

    Code:
     int main(int argc, char  *argv[])
     {
     char filename[SIZE];
     FILE *fp;
      ...
      argv++; 
     strcpy(filename, *argv);
     
      ..
      fp=fopen(filename , "r");
      ...
     return 0; 
    }
    Last edited by kris.c; 07-25-2006 at 08:14 AM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can pass a variable to fopen, you don't have to hard-code the name into your program.

    Code:
    char filename=(argc-1);
    You mean this, right?
    Code:
    strcpy(filename, argv[argc-1]);
    [edit]
    Code:
      argv++; 
     strcpy(filename, *argv);
    Why increment argv? You can just use argv[1].
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I wonder what's wrong with
    fp = fopen( argv[1], "r" );

    Why all the unnecessary copying of the filename to a temporary.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Er... why strcpy()...? Just:
    Code:
    fopen(argv[1], "r");
    And:
    Code:
    strcpy(filename, argv[argc-1]);
    If I enter no commandline arguments, you'll be opening the executable.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It was assumed that you would check for argc to be > 1.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 9
    Last Post: 07-01-2002, 07:50 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM