fopen function call

This is a discussion on fopen function call within the C Programming forums, part of the General Programming Boards category; hello everyone, I can't figure out the correct way to do this. I have a function that gets the current ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45

    fopen function call

    hello everyone,
    I can't figure out the correct way to do this. I have a function that gets the current working directory. I am using fopen to read a file, but want to use CWD function to return cwd:
    Code:
    IPFile = fopen("%s/Sprint_Evdo","r",print_cwd());
    I get to many arguments for fopen.
    Thanks in advance,
    Brad

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You can't do that. fopen() doesn't parse format specifiers like printf() and other such functions. If you're opening a file from the current working directory, then why do you need to specify it? fopen() always looks in the cwd, doesn't it? Maybe I'm just overtired again.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    you mean something like:

    Code:
    char workingDir[BUF_SIZ];
    /* ... */
    sprintf(workingDir, "%s/Sprint_Evdo", print_cwd());
    IPFile = fopen(workingDir, "r");
    ?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    thanks MacGyver and zacs7. fopen does look in cwd. The only problem I was having is the executable may not be in the same folder as the file it is opening to read.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    Well under simple cicumstances, if you're going to open up a file that could be located anywhere, then I would just have a string containing the complete filepath. Maybe pass the filepath as an argument to main. At the very least you wouldn't have to glue it together somewhere and it can be copied and modified. Otherwise, listen to MacGuyver, because fopen works from the cwd on any operating system I've used, and you don't even have to worry about what it is.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    45
    the executable and files will be installed on other machines. I will not be the one installing them, so there is no telling where they will install them. I was trying to piece together a way so no matter where they were installed the application will always know where it is.
    Thanks for the response.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Loading files with a function call
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-10-2007, 07:19 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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