【发布时间】:2011-01-03 23:57:45
【问题描述】:
TCHAR finalpath[MAX_PATH];
GetCurrentDirectory(MAX_PATH,finalpath);
TCHAR filename[] = TEXT("\\lista.txt");
wcscat(finalpath,filename);
wprintf(L"List will be saved to %s", finalpath);
所以这基本上证实了我的 finalpath 确实是 c:\somepath\lista.txt
但是_wfreopen(TEXT(finalpath),TEXT("w"),stdout);
如果我把它改成
_wfreopen(TEXT("c:/somepath/lista.txt"),TEXT("w"),stdout);
然后一切正常,为什么以及如何让它接受我的 finalpath arg?
谢谢
【问题讨论】:
-
我认为问题在于
GetCurrentDirectory()返回的路径没有被反斜杠终止。在添加filename字符串之前,您需要额外调用wcscat()来添加反斜杠。 -
@thkala:
filename中有一个前导反斜杠。另外,这个问题会出现在wprintf输出中。 -
BTW:如果您希望代码在 ANSI 和 Unicode 下都可以工作,则需要为
_tcscat、_tprintf和_tfreopen。 -
你是否有机会为 POSIX 而不是 Win32 编译?
-
@Lambert:你的回答会是一个很好的评论,我会同意,尽管我觉得这不是解决方案。
标签: c++ c windows winapi string