vc2008 Unicode下的对话框程序。使用GetModuleFileName得到主程序的路径,然后要在主程序目录下由下面的代码段建立一个mywind.txt文档并写入东西。现在ctr + F5通过,但是却没有得到mywind文档。而后面的代码段舍去程序路径直接建立mywind文档并写入东西是可以的。整个过程都涉及到CString到char*的转换!代码段一:俺想在主程序的目录下新建一个mywind文档并写入东西,但不行
{
CString sPath;
int nPos;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer (); nPos=sPath.ReverseFind(_T('\\'));
sPath=sPath.Left (nPos);
CString lpszFile = sPath + _T("\\mywind.txt"); USES_CONVERSION;
LPTSTR lpBuffer = lpszFile.GetBuffer();
char* ch = W2A(lpszFile);
lpszFile.ReleaseBuffer(); Vector<int> vect(3);
vect[0] = 1;
vect[1] = 6;
vect[2] = 5; vect.save(ch);
}代码段二:可以建立文档,但不是本来我想要的。另外也说明了其中用到的字符串转换代码段是可行的
{
CString lp = _T("mywind.txt");
USES_CONVERSION;
LPTSTR lpBuffer = lp.GetBuffer();
char* ch = W2A(lp);
lp.ReleaseBuffer(); Vector<int> vect(3);
vect[0] = 1;
vect[1] = 6;
vect[2] = 5;
vect.save(ch);
}代码段三:是save函数,它用的是std的东西。
inline void Vector<Type>::save(char* filename)
{
std::fstream file; // Open file file.open(filename, std::ios::out); if(!file.is_open())
{
std::cerr << std::endl
<< "Flood Error: Vector template." << std::endl
<< "void save(char*) method." << std::endl
<< "Cannot open vector data file." << std::endl
<< std::endl; exit(1);
}
else
{
std::cout << std::endl
<< "Saving vector to data file..."
<< std::endl;
} // Write file file << size << std::endl; for(int i = 0; i < size; i++)
{
file << vector[i] << " ";
} file << std::endl; // Close file file.close();
}
{
CString sPath;
int nPos;
GetModuleFileName(NULL,sPath.GetBufferSetLength (MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer (); nPos=sPath.ReverseFind(_T('\\'));
sPath=sPath.Left (nPos);
CString lpszFile = sPath + _T("\\mywind.txt"); USES_CONVERSION;
LPTSTR lpBuffer = lpszFile.GetBuffer();
char* ch = W2A(lpszFile);
lpszFile.ReleaseBuffer(); Vector<int> vect(3);
vect[0] = 1;
vect[1] = 6;
vect[2] = 5; vect.save(ch);
}代码段二:可以建立文档,但不是本来我想要的。另外也说明了其中用到的字符串转换代码段是可行的
{
CString lp = _T("mywind.txt");
USES_CONVERSION;
LPTSTR lpBuffer = lp.GetBuffer();
char* ch = W2A(lp);
lp.ReleaseBuffer(); Vector<int> vect(3);
vect[0] = 1;
vect[1] = 6;
vect[2] = 5;
vect.save(ch);
}代码段三:是save函数,它用的是std的东西。
inline void Vector<Type>::save(char* filename)
{
std::fstream file; // Open file file.open(filename, std::ios::out); if(!file.is_open())
{
std::cerr << std::endl
<< "Flood Error: Vector template." << std::endl
<< "void save(char*) method." << std::endl
<< "Cannot open vector data file." << std::endl
<< std::endl; exit(1);
}
else
{
std::cout << std::endl
<< "Saving vector to data file..."
<< std::endl;
} // Write file file << size << std::endl; for(int i = 0; i < size; i++)
{
file << vector[i] << " ";
} file << std::endl; // Close file file.close();
}
给你一段例子代码TCHAR szApp[MAX_PATH] = {0};
GetModuleFileName(NULL, szApp, MAX_PATH); *(_tcsrchr(szApp, _T('\\'))+1) = _T('\0');
_tcscat(szApp, _T("mywind.txt")); AfxMessageBox(szApp);
try
{
TCHAR szText[] = _T("你好 Hello,World!!!");
CFile file;
file.Open(szApp, CFile::modeCreate|CFile::modeWrite);
file.Write(szText, sizeof(szText));
file.Close();
}
catch (CFileException* e)
{
e->ReportError();
e->Delete();
}
俺一般使用
TCHAR a[ 0x200 ];
_tcscpy( a, __wargv[ 0 ] );
.............
我曾经用过以下代码来转换CString。转化都是成功的。但是得到的结果都是一样的!
char* UnicodeToAnsi( const wchar_t* szStr )
{
int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );
if (nLen == 0)
{
return NULL;
}
char* pResult = new char[nLen];
WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );
return pResult;
}
char* CStringToChar(CString str)
{
int nlength = str.GetLength();
int nBytes = WideCharToMultiByte(CP_ACP,0,str,nlength,NULL,0,NULL,NULL);
char* ch = new char[nBytes];
memset(ch,0,nBytes+1);
WideCharToMultiByte(CP_OEMCP,0,str,nlength,ch,nBytes,NULL,NULL); return ch;
}
template <class Type>
inline void Vector<Type>::save(char* filename)
{
std::fstream file; std::locale::global(std::locale(""));//为了解决中文路径名而导致不能正常运行
// Open file file.open(filename, std::ios::out); if(!file.is_open())
{
std::cerr << std::endl
<< "Flood Error: Vector template." << std::endl
<< "void save(char*) method." << std::endl
<< "Cannot open vector data file." << std::endl
<< std::endl; exit(1);
}
else
{
std::cout << std::endl
<< "Saving vector to data file..."
<< std::endl;
} // Write file file << size << std::endl; for(int i = 0; i < size; i++)
{
file << vector[i] << " ";
} file << std::endl; // Close file file.close();
}结贴!!!