Thread: filepath help please,

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    21

    filepath help please,

    Hello, i am writing a program and i want to read from a file and create another and write to it, i already have most of it figured out but my problem is the file path, i cant create it to C:\ as it gives issues so i create them at C:\Users\[my username].

    how can i get the program to get the name of the user and automatically put it there since my computer would obviously have a different name from anyone else's own?

    its not essential but i thought it would be a nice touch if its not something hard to do lol.


    Oh another question, how can i make the program open a text file after i finished reading/writing from it so that the user can see the results? or something similar to that. is there also a function to continue running after the notepad is closed?


    thanks.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    In windows systems you can get the address of any "special" folder as a string...

    SHGetSpecialFolderPath()

    The CSIDL property is described here

    You will need a set of windows headers and libs... If you don't have them with your compiler, these come from the Microsoft SDK

    Code:
    #include <windows.h>
    #include <shlobj.h>
    #include <stdio.h>
    
    // get folder path
    int GetShellFolder(char *pName, int FCode)
      { return SHGetSpecialFolderPath(NULL,pName,FCode,0); }
    
    
    
    int main(void)
      { char path[MAX_PATH];
    
         // get "MyDocuments" folder
         if (GetShellFolder(path,CSIDL_MYDOCUMENTS))
           printf("My Documents are at : %s",path);
         else
           printf("Well that didn't work");
    
       return 0; }
    Please note this is untested code... intended only to give you the general idea how it's done.
    Last edited by CommonTater; 04-03-2011 at 10:27 AM.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    21
    Thank you very much, will see if i can use it, it looks a little more complex than i expected

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Shadow20 View Post
    Thank you very much, will see if i can use it, it looks a little more complex than i expected
    Yes, well... neither C nor Microsoft ever did anything the easy way.

    It's not hard to use... just follow the documentation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Declare Character array with filepath?
    By cool_guy in forum C++ Programming
    Replies: 1
    Last Post: 05-26-2010, 08:53 AM
  2. Getting filepath from string
    By SterlingM in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2009, 12:57 PM
  3. Replies: 2
    Last Post: 08-29-2008, 05:29 AM
  4. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-01-2007, 11:48 PM
  5. Separating filename from filepath
    By aker_y3k in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2003, 12:37 PM