似乎无法通过线路读取TMP文件

Cant seem to read a tmp file via ifstream line by line

本文关键字:线路 读取 TMP 文件      更新时间:2023-10-16

喜欢知道我在做什么错。我有一个文件(file.tmp(,其内容为:c: users documents file file folder folder myfile.txt。我想使用此代码读取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.");
    }

输出即时通讯是:

[4376] Reading: ??
[4376] Reading: ??

我希望它能得到:c:usersdocumentsfilefoldermyfile.txt,但是由于某种原因,我尝试了多种方法,但我似乎不知道怎么了。顺便说一句,我是一个开始程序员。

%s格式指定器期望 const char*(或 const wchar_t*,取决于您的字符编码设置(,但您正在传递 std::string对象。您需要调用其C_ST成员:

CStringA cs;
cs.Format("Reading: %s", s.c_str());
OutputDebugStringA(cs);

或使用Unicode字符编码(似乎是(:

CStringW cs;
cs.Format(L"Reading: %S", s.c_str());
OutputDebugStringW(cs);

请注意,后者使用Microsoft特定的扩展名,即格式指定类型%S,该扩展程序执行了字符编码ANSI编码和Unicode之间的转换。