开发者

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.

The function seems to work fine for ints/doubles, but I have a problem with string data.

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

'1' '2' '3' // These values should go into one vector, (vectorOne)

vectorTwo // Identifier for subset of data for another vect开发者_如何学运维or (vectorTwo)

'4' '5' '6' // These values should go into a different vector

vectorThree // Identifier for subset of data for another vector (vectorThree)

'7' '8' '9' // These values should go into a different vector


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. 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." But intead, it contains 4 to 9. In my code (below), I thought that the line:

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

ensured that the read would stop at a newline (i.e., 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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜