Problems with file stream wfstream

Hi everyone ,
i have the following code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<conio.h>
#include<fstream>
#include<stdio.h>
#include<stdlib.h>
#include <windows.h>
#include <string>


int main (int argc, const char * argv[])
{

	using namespace std;
	fstream f;
	wstring tempos = L"myfile.txt";
	f.open (tempos.c_str(), ios::in | ios::out | ios::binary);

return 0;

}


but whene i compile it , it do not works :
image with pop-ups errors :
https://image.ibb.co/iYpCAv/Screenshot_6.png
thanks for your help in advance :)
Last edited on
more helpful if you could post your full program
as the compiler is telling you, the first argument of fstream::open can only be const char* or std::string, while you're trying to give it a const wchar_t* for some reason... probably because you have a program that was written for Visual Studio and used its non-portable extensions (specifically, this one: https://msdn.microsoft.com/library/8473817e-42a4-430b-82b8-b476c86bcf8a.aspx#basic_fstream__open )
Last edited on
@gunnerfunner : nothing special in the code , i just have a unicode file name , then i want to open it with fstram , that's all :) .
--------------------------------
--------------------------------

@Cubbi :
i have files with unicod string name , there is any solution to open such those files .
beside , if i turn to VC++ this code will works fine ?
thanks ,
--------------------------------
--------------------------------
Is there a reason why you don't use wfstream ?
@Thomas1965 : as you can see in the title , acctually i am using wfstream , always the same probleme .
Also the following code do not work !!! :
f.open(L"myfile.txt", ios::in | ios::out | ios::binary);
Update : i include code snipet
You could pass the name of the file directly to the ifstream ctor:
1
2
const std::string fileName{"F:\\test.txt"};
    std::ifstream inFile{fileName};

or, even more succinctly:
std::ifstream inFile{"F:\\test.txt"};

ps: please don't edit OP and put code in there, it disturbs the flow of the thread and keeps future readers wondering. it's better to make a separate post continuing the thread. thanks
dami81 wrote:
i have files with unicod string name , there is any solution to open such those files. beside , if i turn to VC++ this code will works fine ?

You seem to have a string with UTF-16-encoded file name (if you're lucky). It is just one of many ways to store a Unicode string.
Where are you going to run this program on?
If it's not Windows, convert it to UTF-8 and pass that to fstream's constructor/open as std::string. If it's Windows, use Visual Studio or find some other way to get that file name that doesn't go through Windows's unusable wide string - where did it come from in your program in the first place?
In general, if you want it to be portable between Windows and Linux/MacOS, wait for C++17 or use boost.filesystem.

@Thomas1965 wifstream's constructor/open also expects string/char*/filesystem::path, not wstring/wchar_t
Last edited on
this declaration maybe workinfg fo c++11 .
I have unicode file name , the name of the file is not myFile.txt , it is in unicode string . i want to open that file .
Either use the Microsoft library (Visual Studio) which has the non-standard extension indicated by Cubbi

Or do something like this (this works only if the file with the unicode name exists):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <string>
#include <fstream>
#include <windows.h>
#include <cstdlib>

std::ifstream open_file( const std::wstring& wchar_file_name ) 
{
    // create the short path name. (the short path name won't contain
    // characters that can't be represented as char)
    wchar_t short_path[MAX_PATH] {} ;
    ::GetShortPathNameW( wchar_file_name.c_str(), short_path, MAX_PATH ) ;

    // convert the short path to utf-8
    char utf8_path[MAX_PATH] {} ;
    std::wcstombs( utf8_path, short_path, MAX_PATH ) ;

    // open the file normally and return it
    return std::ifstream(utf8_path) ; // note: file streams are movable
}

int main() // usage example
{
    std::wstring tempos = L"myfile.txt";
    std::ifstream file = open_file(tempos) ;
}
thank you very much JLBorges ;
thank you all , guys ;
problem solved (y) ;)
Topic archived. No new replies allowed.