Thread: File output location specification...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    84

    File output location specification...

    Hi,
    I am using the following code, which successfully writes a file to the location below...

    Code:
    FILE *fp = fopen("/Users/me/Desktop/database456abcXa.txt", "w");
    
    if (fp != NULL)
    {
    	fprintf(fp, "Hello, world. This is a file output... Hopefully.");
    	fclose(fp);
    }
    But I would like the user to be able to specify where to save the file...
    so rather than giving it "/Users/me/Desktop/database456abcXa.txt"

    I would like to give it values (defined elsewhere), to be something more like:
    fopen( Directory1 "/" Directory2 "/database456abcXa.txt", "w");
    (you get the idea...)
    How do I do this...
    thanks!

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Naturally, the value of Directory1 would be a string...

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Exactly as you said in your example. You can fopen() directories like "~/Desktop/whatever.txt" or "C:/Documents and Settings/Me/Desktop/whatever.txt"

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You can cwd to a different location to make your relative location different.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by master5001 View Post
    You can cwd to a different location to make your relative location different.
    That usually means invoking a system command, which is unportable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Yes, but what if rather than predefining the location such as: "~/Desktop/whatever.txt"
    I want the program to first take user input, assigned to variable INPUT, and then write the file to:
    "~/INPUT/whatever.txt"

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    And I would prefer to do this without a system command such as cwd...

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ask the user for the path as where to store it. Save that in a string buffer. Pass string buffer to fopen.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I mean use your operating system's method for changing the current working directory. I may overly assume basic knowledge of computers when making such comments.

    Windows. In linux there are a couple methods that come to mind. Or I suppose you can use system() to do your bidding, but that is nasty.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Why not use sprintf()?

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    That's what I want to do...
    what would the formatting of that look like in terms of:
    "FILE *fp = fopen("/Users/Desktop/database.txt", "w");

  12. #12
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    master5001, i know how to change directories, but is it not possible to do with a:
    Take user input, assigned to variable INPUT, and then write the file to:
    "~/INPUT/whatever.txt"
    formatting?

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You're killing me, Smalls.

    Example:
    Code:
    size_t get_user_file_path(char *buffer, size_t size, const char *user)
    {
      return snprintf(buffer, size, "/%s/Desktop/database.txt", user);
    }

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    What?
    Sorry, new to this...

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by hebali View Post
    master5001, i know how to change directories, but is it not possible to do with a:
    Take user input, assigned to variable INPUT, and then write the file to:
    "~/INPUT/whatever.txt"
    formatting?
    Since that's what we've been telling you to do, my guess is that it is possible. Use sprintf (or even snprintf, below) to create your filename string. sprintf works just the same as printf, in terms of the format, but instead of writing to the screen it writes to a char array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM