Jump to content
  • Advertisement
Sign in to follow this  
Chrono1081

Stupidly stuck on writing to a file :(

This topic is 3073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys.

I have a question and I cannot find the answer for the life of me (yes I've googled and searched and wiki'd).

I have to write a simple text file to a users desktop. Thats no problem but I'm stuck on how to make it write to whatever user is currently running the programs desktop.

Say I have this for example:

output.open("C:\\Documents and Settings\\John\\Desktop\\textFile.txt");

What can I put in place of "John" to have the path relative to the current user who is logged in? I've tried all kinds of stuff but I can't get it to work correctly.

For example, if Mary logs in then the textFile.txt will write to her desktop.

Does anyone know how to make this work? Any help would be greatly appreciated.

I know what I'm looking for, I just can't find it :/ Its like how on *ix systems you have a ~Desktop or something like that to show a relative file path. I know windows has ..\Desktop but I just can't get it to work in my code : /

EDIT: Sorry I forgot to mention this is C++ and its a console application.

[Edited by - Chrono1081 on July 13, 2010 3:16:01 AM]

Share this post


Link to post
Share on other sites
Advertisement
Thank you for the suggestion :)

I'm sorry I forgot to write that it was in C++ console not MFC. Is there any way to get this to work with just C++ console? I'm not familiar with MFC :(

Share this post


Link to post
Share on other sites
You don't need MFC to use that function, it's a standard Win API function. (although the function KulSeran mentioned is for Vista and higher).

This works:


#include <iostream>
#include "Shlobj.h"
using namespace std;

int main(int argc, char* argv[]) {
LPTSTR value = new TCHAR[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL,
CSIDL_DESKTOP,
NULL,
SHGFP_TYPE_CURRENT,
value);
if (result == S_FALSE || result == E_FAIL) {
cout << "failed";
}
else if (result == E_INVALIDARG) {
cout << "invalid arg";
}
else {
cout << value;
}
}

/*Compiled with: "cl desktop.cpp /link Shell32.lib User32.lib"*/


Share this post


Link to post
Share on other sites
That is a Windows API function and has nothing to do with the MFC.
There is no "true" c++ way to query the current user because it has not been designed in a way that would make this possible.
To get the current user you will either need to use the API from the operating system or use a higher level API that enables that functionality on multiple platforms.

Share this post


Link to post
Share on other sites
Thank you all so much for the help :) I'm still confused however :( I'm not sure how to convert the contents of result to a string that I can use as a file path.

Here is the code I have:


#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;string&gt;
#include "Shlobj.h"

using namespace std;

int main(int argc, char* argv[])
{
LPTSTR value = new TCHAR[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_DESKTOP, NULL, SHGFP_TYPE_CURRENT, value);

if(result == S_FALSE || result == E_FAIL)
{
cout &lt;&lt; "failed" &lt;&lt; endl;
}

else if(result == E_INVALIDARG)
{
cout &lt;&lt; "invalid arg";
}

else
{

ofstream outFile;

outFile.open(//Not sure how to get result into a string to put here);


for(int i = 0; i &lt; 21; ++i)
{
outFile &lt;&lt; "Labeled and archived tape "&lt;&lt; i &lt;&lt;" for server." &lt;&lt; endl;
outFile &lt;&lt; "Loaded new tape in slot " &lt;&lt; i &lt;&lt; " for server." &lt;&lt; endl;

}

outFile.close();
}

return 0;
}




Sorry for my ignorance, I'm really unfamiliar with Windows programming. I usually program in Objective-C + Cocoa.

I looked for an HRESULT to string converter code snippet but everything I found appears to be Direct X related : /

Share this post


Link to post
Share on other sites
HRESULT is meant to tell you if a Windows API call was executed successfully or not. You should use the FAILED macro to check if everything is okay.

Now concerning getting the actual path:
When you look at the documentation of SHGetFolderPath, it says that the function copies the string describing the path into pszPath. Your value variable should already contain the path, although you might want to change your code a bit:

TCHAR value[MAX_PATH];
HRESULT result = SHGetFolderPath(NULL, CSIDL_DESKTOP, NULL, SHGFP_TYPE_CURRENT, &value[0]);
if(FAILED(result))
{
cout << "failed" << endl;
}
else
{
ofstream outFile;
outFile.open(value);
...




I hope this helps :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Chrono1081
I'm still confused however :( I'm not sure how to convert the contents of result to a string that I can use as a file path.

Did you try the code i gave you? It prints the variable value to cout (the console), where value is defined as a C-style string.

If you want the value as a std::string you will have to convert it. So then you can append your file name to the end: pathToDesktop + '\\' + fileName.
Or you could just use the C string function strcat to append the file name.

Share this post


Link to post
Share on other sites
I tried the code you provided but I only get a memory address.

I also tried the code that SiS-Shadowman provided and I only get a memory address as well : / I can't figure out what is going wrong :(

Share this post


Link to post
Share on other sites
This code works for me. Compiled with Code::Blocks. Tell me if it solves your problem.


#include <iostream>
#include "Shlobj.h"
using namespace std;

const int SHGFP_TYPE_CURRENT = 0;

int main(int argc, char* argv[])
{
LPTSTR value = new TCHAR[MAX_PATH];

HRESULT result = SHGetFolderPath(NULL,
CSIDL_DESKTOP,
NULL,
SHGFP_TYPE_CURRENT,
value);

if (result == S_FALSE || result == E_FAIL)
cout << "failed";

else
cout << value;

delete [] value;
}





The string with the path to the desktop is value, not result. You will have to add the name of the file you want to write in to value and you're done.

As XTAL256 said, it would be wiser and easier if you stored the path to the desktop in a std::string.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!