将TEXT行读入字符

时间:2012-05-15 20:50:52

标签: c++

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

        char temp_char  = '\0';
        char _char = '\0';
        while(1)
        {
            _places.get(temp_char);
            if(temp_char == '\t')
                break;

            _char += temp_char;

            cout << _char;
        }

基本上,它应该从文件的开头读取,直到它与制表符接触。不幸的是,它永远不会停止,并且_char作为大量随机字符出现是没有意义的。

我查看了TXT文件,它是正确的编码和所有,并且标签是文件中的普通标签字符,但无论出于何种原因我都无法使用get或&gt从流中读取它;&GT;

我现在已经进行了一个小时的实验,看了很多网站寻求帮助,我无处可去......这一切都在给我压力......

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

以下是文件的打开位置:

                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);
                }

1 个答案:

答案 0 :(得分:2)

首先,_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可以返回int时,请小心使用get()方法。