best way to get filename

This is a discussion on best way to get filename within the C Programming forums, part of the General Programming Boards category; Here's my question. Not really a problem, because I know an alternative, but I don't really like it. This program ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    best way to get filename

    Here's my question. Not really a problem, because I know an alternative, but I don't really like it. This program is going to need to get a filename (directory also for fopen) from the user. The thing is, the program (obviously) has to be run from it's directory, do how would I get the filename from the user without making them type:

    C:\dir1\dir2\dir3\file.txt

    That is too sloppy. Is there a way I can run the program while in the chosen directory without being in the directory of the executable file? Thanks.

    --Garfield

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Try the system command if you are using MSVC++

    system("cd c:\path\name");

    The system command allows you to use the command shell within your program.

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    I don't understand, Troll_King. Where would I put this code, and what would it do? Would it allow me to run the program anywhere, not just the program's directory? Thanks.

    --Garfield

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,447
    > the program (obviously) has to be run from it's directory
    Not so

    \foo\bar\prog.exe is also a valid thing to type.

    A better way is to put \foo\bar in your environment variable, then you can just type prog.exe whereever you are (or even just prog)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Not sure what the question meant, but anyway if you go to the command prompt and type the word: path

    It will show you the execuable path. Someone asked this yesterday. I think the command to reset the path is something like: path=$path:c:\path\to\add\you\decide

    This gets reset every time your comp is turned off unless you change this in the appropriate batch file.

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > A better way is to put \foo\bar in your environment variable

    How would you do that? And wouldn't that restrict the user to just use that directory to retreive the files? What would the code be for this? Thanks.

    --Garfield

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    What damn OS are you using? This is system dependant. Okay I looked it up for MS OS's.

    say this is the path that you want to add to the executable path:
    C:\dir1\dir2\dir3\file.txt

    Go to the command prompt. Type 'path' to see the current path. Than type:
    path C:\DIR1\DIR2\DIR3\;%PATH%

    Than hit enter. Than type path again and see the change. Than type:
    file.txt

    It will execute.

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, if I go to path and set the path of the exe file (prog.exe) and then I don't have to be in the folder of the exe to run it. Well, that's good and all...but for just me. What if I don't want to run this just on my computer? Thanks.

    --Garfield

    I'm using Windows.

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Not sure what you mean again. You want the path added to any computer that runs your program so that any computer can execute the program without having to be in the directory? If this is the case than you will likely have to write an insallation module.

  10. #10
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > If this is the case than you will likely have to write an insallation module.

    Yeh, you answered my question. Thanks.

    --Garfield

  11. #11
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    or you could add system("pathjunkhere"); to your program.

  12. #12
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    > or you could add system("pathjunkhere"); to your program.

    What??? How would this work? What does this system function do? And, I want to run the program outside of its directory. Thanks.

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you can get the filename input and then with a small amount of parsing of argv[0] (which is the full path of your executable) you can extract the path and strcat() the filename onto it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Stoned_Coder, I'm not sure if I understand what you suggest. What do you mean by
    >you can get the filename input

    I'm not sure if you guys are understanding my question. This is what I want to do:

    path for txt file:
    C:\This\Is\The\Path\For\letter.txt

    path for prog.exe file:
    C:\This\Is\The\prog.exe

    I want the user to be able to do this:

    C:\This\Is\The\Path\For > prog letter.txt

    So, I want to run the exe outside of its own dir. Thanks.

    --Garfield

  15. #15
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Any suggestions fellas'? Thanks.

    --Garfield

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-11-2009, 01:49 AM
  2. Pass Filename to another function
    By awesmesk8er in forum C Programming
    Replies: 9
    Last Post: 10-24-2008, 01:43 PM
  3. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 03:36 AM
  4. Time as filename
    By rkooij in forum C Programming
    Replies: 8
    Last Post: 03-02-2006, 08:17 AM
  5. Replies: 3
    Last Post: 01-25-2006, 09:30 PM

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