Solved

create path

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now