Solved

create path

Posted on 2014-03-16
5
285 Views
Last Modified: 2014-03-16
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.
0
Comment
Question by:mastiSoft
  • 3
  • 2
5 Comments
 
LVL 7

Expert Comment

by:tampnic
ID: 39932373
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
0
 
LVL 1

Author Comment

by:mastiSoft
ID: 39932388
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 aerrors for line 3
0
 
LVL 7

Expert Comment

by:tampnic
ID: 39932391
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
0
 
LVL 7

Accepted Solution

by:
tampnic earned 345 total points
ID: 39932402
The following code compiles under either character set. It uses a typedef on std::basic_string to allow the tstring type to swap between the STL string and wstring types depending on what TCHAR is set up as by the preprocessor.

#include "stdafx.h"
#include "windows.h"
#include "Shlobj.h"
#include <string>

using namespace std;

typedef std::basic_string<TCHAR> tstring;
typedef std::basic_ofstream<TCHAR> tofstream;

int _tmain(int argc, _TCHAR* argv[])
{
	TCHAR folder_path[MAX_PATH];
	HRESULT result = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, folder_path);
	tstring path = ((tstring)folder_path) + _T("\\myfile.bin");

	tofstream myoutfile(path, std::ofstream::out|std::ios_base::app);
	myoutfile << _T("Some text in a file");
	myoutfile.close();

	return 0;
}

Open in new window


Cheers,
   Chris
0
 
LVL 1

Author Closing Comment

by:mastiSoft
ID: 39932466
Thank you Chris,
Now when I looked at your code I found that I forgot include <string>.
Every thing working fine now.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question