Solved

create path

Posted on 2014-03-16
5
286 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 116
allswap challenge 6 134
Where are the c++ header files to use in Eclipse? 4 53
Problem to copy file 14 53
This article will show, step by step, how to integrate R code into a R Sweave document
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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