unicode下得到的主程序路径不能转化成char*!???

雪野狼孤 2010-07-17 12:21:17
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();
}
...全文
166 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪野狼孤 2010-07-17
  • 打赏
  • 举报
回复
代码二编译后得到的exe程序不管拷贝到什么地方,运行的话都在exe所在的目录下生成mywind文档。
雪野狼孤 2010-07-17
  • 打赏
  • 举报
回复
在线等!!!
雪野狼孤 2010-07-17
  • 打赏
  • 举报
回复
问题解决!!

在代码加入std::locale::global(std::locale(""));即可:
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();
}


结贴!!!
雪野狼孤 2010-07-17
  • 打赏
  • 举报
回复
网上搜了一艘,案例还是挺多的!发现是由于std::fstream 和中文路径的关系所致。
雪野狼孤 2010-07-17
  • 打赏
  • 举报
回复
一路跟踪下来,发现确实是因为路径中有中文才导致不能得到正确的结果。我把project放在一个路径中没有中文的目录中。却得到了想要的结果。

这个怎么办??
用户 昵称 2010-07-17
  • 打赏
  • 举报
回复
可以任意转换。
俺一般使用
TCHAR a[ 0x200 ];
_tcscpy( a, __wargv[ 0 ] );
.............
bdzwj 2010-07-17
  • 打赏
  • 举报
回复
用CW2A 或者 CT2A类转换
雪野狼孤 2010-07-17
  • 打赏
  • 举报
回复
难道说因为路径中有中文的缘故吗?如果是,如何跟踪?
雪野狼孤 2010-07-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 lazy_2010 的回复:]
1、跟踪一下,在 W2A 之后是否路径中的中文字符因为 codepage、locale 之类的原因被转换成乱码了;如果是,尝试使用 CStringA,或者调用 WideCharToMultiByte 解决

2、看看写文件的路径,是否存在权限问题;
[/Quote]

我曾经用过以下代码来转换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;
}
雪野狼孤 2010-07-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 visualeleven 的回复:]
在Unicode编码方式下,不是一样可以写吗,为什么一定要将CString转成char*
给你一段例子代码

C/C++ code

TCHAR szApp[MAX_PATH] = {0};
GetModuleFileName(NULL, szApp, MAX_PATH);

*(_tcsrchr(szApp, _T('\\'))+1) = _T('\0');
……
[/Quote]
关键是我提到的代码三中用的是char*!!!
lazy_2010 2010-07-17
  • 打赏
  • 举报
回复
1、跟踪一下,在 W2A 之后是否路径中的中文字符因为 codepage、locale 之类的原因被转换成乱码了;如果是,尝试使用 CStringA,或者调用 WideCharToMultiByte 解决

2、看看写文件的路径,是否存在权限问题;
Eleven 2010-07-17
  • 打赏
  • 举报
回复
在Unicode编码方式下,不是一样可以写吗,为什么一定要将CString转成char*
给你一段例子代码

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();
}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧