Solved

create path

Posted on 2014-03-16
5
287 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
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…
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.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

690 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