Tags

,

The Problem
In C++, I used to use ifstream and ofstream to read and write file, to determine file existence.

But in recent project, as the file name may be unicode, we need use wide string, wchar_t.  Just find out that ifstream and ofstream doesn’t work with unicode file name.

The program is simple: it checks whether one file exists, if not, write some text into it, if exists, do nothing.
The code that doesn’t work

//<![CDATA[
if(showAdsense){
document.write("(adsbygoogle = window.adsbygoogle || []).push({});”)
}
//]]>

void codeDoesnotwork()
{
 wchar_t* filePath =L"E:\\tmp\\test.txt";
 ifstream infile(filePath);
 if (!infile.is_open() || 
  !infile.good())
 {
  cout << "file doesn't exist" << endl;
 }
 else
 {
  // it alwalys goes here, even the file doesn't exist.
  cout << "file exists" << endl;
 }

 // ofstream << or write doens't work.
 ofstream ofs(filePath,ios::out | ios::trunc);
 ofs<< "hello world!" << endl;
 ofs.write("1", 1);
 ofs.close();
}

This is because ifstream and ofstream doesn’t work with wide string.
The code that works

using namespace std;
typedef unsigned int uint;
void codeThatWorks()
{
 wchar_t *installFolder=L"E:\\tmp";
 wchar_t *fileName = L"test.txt";
 uint fileNameBufferSize = wcslen(installFolder) + wcslen(fileName) + 2;

 wchar_t *fileNameBuffer = new wchar_t[fileNameBufferSize];
 assert(fileNameBuffer);
 memset(fileNameBuffer, 0, fileNameBufferSize * sizeof(wchar_t)); 
 swprintf_s(fileNameBuffer, fileNameBufferSize, L"%s/%s", installFolder, fileName);

 FILE* oFile;
 oFile = _wfopen(fileNameBuffer,L"r");
 if(oFile==NULL)
 {
  cout << "file doesn't exist" << endl;
  //FILE* oFile;
  oFile = _wfopen(fileNameBuffer,L"w+");
  fprintf(oFile,"%s", "hello world");  
 }
 else
 {
  cout << "file exists" << endl;
 }
 fclose(oFile);
 delete[] fileNameBuffer;
}

Write Unicode Content to File
If we need write unicode content to file, we should use  _wfopen or wofstream to write wide string to file.
f = _wfopen(COMMON_FILE_PATH,L”w, ccs=UTF-16LE”);

Use Shlwapi.lib PathFileExists
We can use Shlwapi.lib’s PathFileExists to check file existence:
BOOL exist = PathFileExists(fileNameBuffer);
Also in order to use Shlwapi.h, we need add Shlwapi.lib library by right clicking the project -> “Configuration Properties” -> “Linker” -> “Input” -> “Additional Dependencies” -> “Edit” -> type “Shlwapi.lib” in the text box.

Resource
MSDN fopen, _wfopen
Wrote to a file using std::wofstream

via Blogger http://ift.tt/1fCdGu6

Advertisements