[英]Cant seem to read a tmp file via ifstream line by line
Just like to know what I am doing wrong. 只是想知道我在做什么错。 I have a file (file.tmp) that has content of : c:\\users\\documents\\file\\folder\\myfile.txt.
我有一个文件(file.tmp),其内容为:c:\\ users \\ documents \\ file \\ folder \\ myfile.txt。 I would like to read the tmp file with this code:
我想用以下代码读取tmp文件:
std::ifstream istream(csTempPath);
std::string s;
if (istream.is_open()){
int i = 1;
while (std::getline(istream, s))
{
CString cs;
cs.Format(L"Reading: %s", s);
OutputDebugString(cs);
i++;
}
istream.close();
}
else{
OutputDebugString(L"Could not read the temp file.");
}
The output Im getting is this: 我得到的输出是这样的:
[4376] Reading: ??
[4376] Reading: ??
I expect it to get this: c:\\users\\documents\\file\\folder\\myfile.txt
but for some reason I get that,I tried variety of ways but I cant seem to know whats wrong. 我希望它能得到这个:
c:\\users\\documents\\file\\folder\\myfile.txt
但是由于某种原因,我尝试了多种方法,但是我似乎不知道出了什么问题。 BTW Im a beginning programmer. 顺便说一句,我是一个初学者。
The %s
format specifier expects a const char*
(or const wchar_t*
, depending on your character encoding settings), yet you are passing a std::string
object. %s
格式说明符期望使用const char*
(或const wchar_t*
,具体取决于您的字符编码设置),但是您正在传递std::string
对象。 You need to invoke its c_str member: 您需要调用其c_str成员:
CStringA cs;
cs.Format("Reading: %s", s.c_str());
OutputDebugStringA(cs);
Or, when using Unicode character encoding (as you seem to be): 或者,当使用Unicode字符编码时(如您所愿):
CStringW cs;
cs.Format(L"Reading: %S", s.c_str());
OutputDebugStringW(cs);
Note that the latter uses a Microsoft-specific extension, the format specifier type %S
, that performs character encoding conversions between ANSI encoding and Unicode. 请注意,后者使用Microsoft特定的扩展名,格式说明符类型
%S
,它执行ANSI编码和Unicode之间的字符编码转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.