将 TXT 台词读成字符

Reading TXT Lines into Character

本文关键字:字符 TXT      更新时间:2023-10-16

我现在完全没有想法。基本上,我想读取文本文件中一行的一部分,但是这段代码不起作用,我不知道为什么:

        char temp_char  = '';
        char _char = '';
        while(1)
        {
            _places.get(temp_char);
            if(temp_char == 't')
                break;
            _char += temp_char;
            cout << _char;
        }
基本上,它

应该从文件的开头读取,直到它与制表符接触。不幸的是,它永远不会停止,_char以大量毫无意义的随机字符出现。

我已经查看了 TXT 文件,它是正确的编码和所有,并且制表符是文件中的普通制表符,但由于某种原因,我无法使用 get 或>> 从流中读取它。

我已经尝试了一个小时,查看了许多网站寻求帮助,但我一无所获......这一切都在给我带来压力...

编辑:如果这不是问题,我也可以提供程序代码的其余部分,但它应该是。

以下是打开文件的位置:

                ifstream _places;
                _places.open("ECC.txt");
                if (_places.fail())
                {
                    cout << "Could not load text file, please make sure ECC.txt is in the same folder.";
                    _getch();
                    exit(1);
                }

首先,_char是 std::string, char[] 还是 char* ?

其次,我会使用

temp_char = _places.get()

std::string temp;
getline(_places,temp)
for (unsigned int i=0; i<temp.size(), ++i)
    if (temp[i] == 't')
        break;
    _char += temp[i] // assuming, and REALLY HOPING _char is a std::string 

小心 ifstream 中的 get() 方法,当它们可以返回 int 时。