Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 117,527 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,963 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Read a txt file from a deaktop

 
Reply to this topicStart new topic

Read a txt file from a deaktop

webmin
post 28 Jun, 2008 - 07:12 AM
Post #1


D.I.C Head

**
Joined: 21 May, 2008
Posts: 95


My Contributions


Ok how can I get my program in C to read a txt file from a variable location like the desktop. Basically I want the user to have the txt file on their desktop, but everyone's path to their desktop is different. Any suggestions please.
User is offlineProfile CardPM

Go to the top of the page


gabehabe
post 28 Jun, 2008 - 07:16 AM
Post #2


T3H R0XX0R!

Group Icon
Joined: 6 Feb, 2008
Posts: 3,374



Thanked 68 times

Dream Kudos: 2175

Expert In: (X)HTML, CSS, Batch Scripting, C, C++

My Contributions


I gave some advice on something like this before...

Here is the code I used, and tested (it works!)
cpp
#include <windows.h>
#include <iostream>
#include <shlobj.h>

using namespace std;

int main ()
{
// Allocate a pointer to an Item ID list
LPITEMIDLIST pidl;

// Get a pointer to an item ID list that represents the path of a special folder
HRESULT hr = SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOPDIRECTORY, &pidl);

// Convert the item ID list's binary representation into a file system path
char szPath[_MAX_PATH];
BOOL f = SHGetPathFromIDList(pidl, szPath);

// Allocate a pointer to an IMalloc interface
LPMALLOC pMalloc;

// Get the address of our task allocator’s IMalloc interface
hr = SHGetMalloc(&pMalloc);

// Free the item ID list allocated by SHGetSpecialFolderLocation
pMalloc->Free(pidl);

// Free our task allocator
pMalloc->Release();

// print it (you can do whatever)
cout << szPath;

cin.get ();
return EXIT_SUCCESS;
}

Hope this helps smile.gif
User is online!Profile CardPM

Go to the top of the page

webmin
post 3 Jul, 2008 - 06:09 PM
Post #3


D.I.C Head

**
Joined: 21 May, 2008
Posts: 95


My Contributions


That's great, but way too advanced. Sorry, is there anyway this can be done in C.

Sorry I wasn't more specific.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 3 Jul, 2008 - 07:38 PM
Post #4


Wet D.I.C.

Group Icon
Joined: 10 May, 2007
Posts: 5,420



Thanked 35 times

Dream Kudos: 2350

Expert In: Goofing Off

My Contributions


I don't understand why the location makes it difficult? Just use fopen & feed it the full path...?

Can you post what you have tried & why it doesn't work?
User is offlineProfile CardPM

Go to the top of the page

webmin
post 4 Jul, 2008 - 08:34 AM
Post #5


D.I.C Head

**
Joined: 21 May, 2008
Posts: 95


My Contributions


I have tried the full path and that works, however that works only on my computer. I need to make it for a lack of better words portable to any computer that will run my code.

Example: My teacher will run the code on his desktop, and since the path to his desktop is different from the path to my desktop. I need my program to be able to find the path to any users desktop
User is offlineProfile CardPM

Go to the top of the page

kapax
post 4 Jul, 2008 - 11:56 AM
Post #6


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 38



Thanked 1 times
My Contributions


gabehabe,

I haven't checked your code much but I see that it uses windows.h and probably that library helps to find a desktop. Now, this guy has Linux Ubuntu avatar on his profile. Would it be fair to think that he needs a help for Linux? I think it's more likely, because:

To find a Desktop folder on Windows you don't need anything fancy because Desktop is always at the same place, in the user's account folder (is it different on Linux, by the way? I don't remember).



P.S. webmin, please give us your full path and give us some examples of what do other pathes look like. I will see if I can help.

This post has been edited by kapax: 4 Jul, 2008 - 11:57 AM
User is offlineProfile CardPM

Go to the top of the page

webmin
post 4 Jul, 2008 - 05:13 PM
Post #7


D.I.C Head

**
Joined: 21 May, 2008
Posts: 95


My Contributions


Actually, the ubuntu graphic was just something that I thought made a cool avatar. I've tried linux, and like it, unfortunately I just don't have the time or patience to learn how to use it to it's full potential. So I stick with Windows where I'm familiar with everything, until I get a powerful enough second machine to run Fedora Core and or Freespire.

Back on topic though, what I am trying to do is to make my code portable, meaning that my code could find not only my desktop, but anyone's desktop who's using XP, because the path to the desktop relies on the different usernames, I want my code to be able to find it regardless of the username.

let me know if you guys figure out anything. Thanks
3.
User is offlineProfile CardPM

Go to the top of the page

realNoName
post 4 Jul, 2008 - 10:20 PM
Post #8


D.I.C Regular

***
Joined: 4 Dec, 2006
Posts: 290



Thanked 5 times
My Contributions


my first thought was to use windows environment variables and just do something like this

CODE
fstream test("%USERPROFILE%\\Desktop\\myfile.txt");


but that didn't work or at least i couldn't get it to but then i just found a nice little function from c called getenv that will return the value of a system variable so all you will have to do is call that function with userprofile and it will return the path you need and then just add /desktop to it

CODE
getenv("USERPROFILE")



fyi a list of windows variables
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 10/7/08 03:57PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month