Link to home
Start Free TrialLog in
Avatar of mastiSoft
mastiSoft

asked on

create path

Hi,
I need to create path to write binary file. I have path to MyDocuments folder and now I try to do some thing like that MyDocuments/bin.file
CHAR folder_path[MAX_PATH];
HRESULT result = SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, folder_path);
string path = folder_path.c_str() + "/myfile.bin";

Open in new window

this path I'll use in
ofstream outfile(path, std::ios_base::out | std::ios_base::app);

Open in new window

For some reason the way I create path wrong.
Avatar of tampnic
tampnic
Flag of United Kingdom of Great Britain and Northern Ireland image

One thing I notice is that folder_path is declared as a CHAR array, so I wouldn't expect folder_path.c_str() to compile.


CHAR folder_path[MAX_PATH];
HRESULT result = SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, HGFP_TYPE_CURRENT, folder_path);
string path = ((string)folder_path) + "\\myfile.bin";

Open in new window


would work but be careful of the directory separator, I haven't tested the validity of the path so maybe only a single backslash "\" is required .

Cheers,
   Chris
Avatar of mastiSoft
mastiSoft

ASKER

Hi Chris.
It still didn't work. Maybe I forgot to include some thing
#include <iostream>
#include <Windows.h>//to get path in windows
#include <shlobj.h>//to get path in windows
#include <fstream> //to write/read binary files
#include <sys/stat.h>
using namespace std;

Open in new window


line 3 in your code gives aUser generated image
Its probably an ASCII/Unicode thing. What character set are you compiling for?

The only things I included to do a quick test were

#include "stdafx.h"
#include "windows.h"
#include "Shlobj.h"
#include <string>
using namespace std;

Open in new window


Maybe try

string path = ((string)folder_path) + string("\\myfile.bin");
// or
string path = ((string)folder_path) + L"\\myfile.bin";
// or
string path = ((string)folder_path) + _T("\\myfile.bin");

Open in new window


to see if any of those compile with your preprocessor directives

Cheers,
   Chris
ASKER CERTIFIED SOLUTION
Avatar of tampnic
tampnic
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you Chris,
Now when I looked at your code I found that I forgot include <string>.
Every thing working fine now.