无法初始化 ifstream "读取字符串字符时出错"

can't initialize ifstream "Error reading characters of string"

本文关键字:字符 出错 字符串 读取 初始化 ifstream      更新时间:2023-10-16

下面给了我一个奇怪的消息:该消息显示在ifstream文本中(悬停时)path是一个完整的windows风格的路径,例如:"C:\t.txt"

void checkInput(string path)
{
ifstream text(path);
    // "...<Error reading characters of string.>..."
if (text.is_open())
{
    if (text.good())
               ...

完整信息:

{_Filebuffer={_Set_eback=0xcccccccc <Error reading characters of string>.
_Set_egptr=0xcccccccc <Error reading characters of string>. ...} }
std::basic_ifstream<char,std::char_traits<char>> "

我试着用char*代替字符串,并用string.c_str。不好。

对于答案,尽管我仍然收到这条消息,但代码似乎仍在运行。之前我的代码中有另一个错误。

我想过删除它,但知道这条消息不会阻止代码运行可能会有所帮助。

  1. 如果您使用的路径像您写的"C:\t.txt"一样,这是不正确的。Use必须使用双反斜杠,如"c:\\t.txt"
  2. 如果使用绝对路径,则检查文件是否存在这样的路径
  3. 如果您使用相对路径,这取决于您在哪里运行代码:从VS(调试模式)还是只运行可执行文件