简体   繁体   English

C ++:从外部文件读取数据; 我的代码在文件结束前停止读取的问题

[英]C++: reading data from an external file; problem with my code for stopping read before end of file

To use code I have written for performing calculations, I need to read in data (numbers and strings) from an external text file and store them in vectors of either strings or ints/doubles. 要使用我编写的用于执行计算的代码,我需要从外部文本文件中读取数据(数字和字符串),并将它们存储在字符串或整数/双精度数的向量中。 I have written a template function for doing this. 我已经为此编写了模板函数。 CashCow, Howard Hinnant, and wilhelmtell kindly helped with a previous problem. CashCow,Howard Hinnant和wilhelmtell曾帮助解决了先前的问题。

The function seems to work fine for ints/doubles, but I have a problem with string data. 该函数对于int / doubles似乎工作正常,但是我对字符串数据有问题。

I need data from ONE line of my external file to go into a vector, but the function reads in multiple lines. 我需要来自外部文件的一行的数据才能进入向量,但是该函数读取多行。 Here's what I mean. 这就是我的意思。 Let's say this is what is in the external text file (below): 假设这是外部文本文件中的内容(如下):


vectorOne // Identifier for subset of data for one vector vectorOne //一个向量的数据子集的标识符

'1' '2' '3' // These values should go into one vector, (vectorOne) '1''2''3'//这些值应进入一个向量(vectorOne)

vectorTwo // Identifier for subset of data for another vector (vectorTwo) vectorTwo //另一个向量(vectorTwo)的数据子集标识符

'4' '5' '6' // These values should go into a different vector '4''5''6'//这些值应放入不同的向量

vectorThree // Identifier for subset of data for another vector (vectorThree) vectorThree //另一个向量(vectorThree)的数据子集标识符

'7' '8' '9' // These values should go into a different vector '7''8''9'//这些值应放入不同的向量


If I look for a data subset identifier/label (like vectorOne), I want only the data on the next line to go into my result vector. 如果我寻找数据子集标识符/标签(例如vectorOne),则只希望将下一行中的数据放入结果向量中。 The problem is that ALL data below the identifier/label are ending up in the result vector. 问题在于,标识符/标签下的所有数据最终都在结果向量中。 So if vectorTwo is what I want, I expect my result vector to contain the elements, "4, 5, 6." 因此,如果我想要vectorTwo,我希望我的结果向量包含元素“ 4、5、6”。 But intead, it contains 4 to 9. In my code (below), I thought that the line: 但是intead包含4到9。在我的代码(如下)中,我认为这一行:

while ( file.get() != '\n' );

ensured that the read would stop at a newline (ie, after each line of data). 确保读取将在换行处停止(即,在每行数据之后)。

I would be very grateful for any suggestions as to what is going wrong. 对于出现问题的任何建议,我将不胜感激。

Here's the code (for clarity, I configured it for strings): 这是代码(为清楚起见,我为字符串配置了代码):

#include <algorithm>  
#include <cctype>     
#include <istream>
#include <fstream>
#include <iostream>    
#include <vector>
#include <string>
#include <sstream>
#include <iterator>

using namespace std; 

template<typename T>  
void fileRead( std::vector<T>& results, const std::string& theFile, const std::string& findMe, T& temp )  
{   
    std::ifstream file( theFile.c_str() ); 
    std::string   line;

    while( std::getline( file, line ) )
    {
        if( line == findMe )
        {
            do{
                std::getline( file, line, '\'' );  
                std::getline( file, line, '\'');

                std::istringstream myStream( line );

                myStream >> temp;
                results.push_back( temp );
            } 
            while ( file.get() != '\n' );
        }
    }
}


int main () 
{
    const std::string theFile               = "test.txt";  // Path to file
    const std::string findMe                = "labelInFile"; 
    std::string temp;

    std::vector<string> results;

    fileRead<std::string>( results, theFile, findMe, temp );

    cout << "Result: \n";
    std::copy(results.begin(), results.end(), std::ostream_iterator<string>(std::cout, "\n")); 

    return 0;
}

Thanks 谢谢

Looks to me like you may have a problem mixing getline and get . 在我看来就像你可能有一个问题,混合getlineget

When you've read the name of the vector you want, you start reading the parts between single quotes. 读取所需向量的名称后,就开始读取单引号之间的部分。 Once you've read whatever is between single quotes, you check to see if the next character is an end of line. 阅读完单引号之间的内容后,请检查下一个字符是否在行尾。 If there's anything else on the line before the newline, the test fails and it reads whatever is between the next pair of single quotes. 如果换行符之前的行还有其他内容,则测试将失败,并且它将读取下一对单引号之间的内容。 If there's a comment at the end of the line, or a space, or anything at all past the last single quote, what you've got will fail. 如果在行的末尾有注释,或者有空格,或者在最后一个单引号之后都没有任何内容,那么您所得到的将失败。

Try reading the whole line into a string, and then reading it as a stringstream. 尝试将整行读取为字符串,然后将其读取为字符串流。 That way, you can't go past the end of line. 这样,您就无法越过行尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM