Thread: File output location specification...

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by hebali View Post
    What?
    Sorry, new to this...
    Code:
    int main(void)
    {
      char path[MAX_PATH];
      FILE *db;
     
      get_user_file_path(path, sizeof(path), "root");
     
      db = fopen(path, "w");
      
      ...
     
      fclose(db);
      return 0;
    }

  2. #17
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Yes, I guess so.
    But I didn't understand the formatting of Master's example.
    Sorry

  3. #18
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    The only formatting flag I had in there was a single %s. Which simply places a string. I used snprintf() so that one can avoid buffer overflow.

    snprintf([string], [size of the string], [format], ...)

  4. #19
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    in trying to compile
    Code:
    size_t get_user_file_path(char *buffer, size_t size, const char *user)
    {
      return snprintf(buffer, size, "/%s/Desktop/database.txt", user);
    }
    I get error "nested functions are disabled, use -fnested-functions to re-enable"
    what does this mean?

  5. #20
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Don't make a nested function, silly. Put it outside of main.

  6. #21
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    What is that function:
    Code:
    size_t get_user_file_path(char *buffer, size_t size, const char *user)
    {
      return snprintf(buffer, size, "/%s/Desktop/database.txt", user);
    }
    It doesn't take user input...

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It is assumed, by this function, that you have already gotten the input and it will be passed in as the third parameter.

  8. #23
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    So why call it?

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Dude, because it will create your "filename."
    Get input path.
    Pass that to the function.
    Function returns the path + your file, which you can directly pass 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.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by hebali View Post
    So why call it?
    Because you said you wanted the user to be able to specify what directory the file should be written to.

  11. #26
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Sorry, I just caught onto what that element is for...
    I was assuming I would supply my own string getting function, but then you handed me the above code, with which I was unfamiliar, and I thought this was a better way to get it...
    But now I understand what it does...

  12. #27
    Registered User
    Join Date
    Feb 2008
    Posts
    84
    Thanks for your help

  13. #28
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If my example is not suitable for exactly what you are doing be less general and tell me exactly what it is you want.

    The only different way of doing this would be:

    Example:
    Code:
    char buffer[MAX_PATH] = "/"
    
    strncat(buffer, user, MAX_PATH);
    strncat(buffer, "/Desktop/database.txt", MAX_PATH);
    But that is more lines of code... *shrug*

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