Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

c++ - C ++ string stream issue: getline doesn't work with stringstream

I wrote a program that takes a file and reads it into a stringstream field in a class, and now I'm trying to interact with it. The problem is that when reading sequentially from several methods, one of the methods gives an error, or simply does not work. I guess the problem is how I read the file, how should I improve it?

There is my class:

class MatReader
{
protected:
    ...
    stringstream text;
    ...
    string PhysicsMaterial;
    string Diffuse;
    string NMap;
    string Specular;

public:
    /// <summary>
    /// Read all lines in .mat document by string
    /// </summary>
    void ReadAllLines(string file_path);
    /// <summary>
    /// Getting PhysicsMaterial property
    /// </summary>
    string getPhysMaterial();
    /// <summary>
    /// Getting diffuse file path
    /// </summary>
    string getDiffuseLocation();
};

And there is my implementation file:

#include "MaterialHandler.h"

void MatReader::ReadAllLines(string mat_file)
{
    ifstream infile(mat_file);
    string str;
    if (infile.is_open())
    {
        ofile = true;
        while (!infile.eof())
        {
            getline(infile, str);
            text << str+"
";
        }
    }
    else
        throw exception("[ERROR] file does not exist or corrupted");
}

string MatReader::getPhysMaterial()
{
    string line;
    vector<string> seglist;
    try
    {
        if (ofile == false)
            throw exception("file not open");
    
        while (getline(text, line, '"'))
        {
            if (!line.find("/>"))
                break;
            seglist.push_back(line);
        }
        for (uint16_t i{}; i < seglist.size(); i++)
        {
            if (seglist[i-1] == " PhysicsMaterial=")
            {
                PhysicsMaterial = seglist[i];
                return seglist[i];
            }
        }
        line.clear();
        seglist.clear();
    }
    catch (const std::exception& ex)
    {
        cout << "[ERROR]: " << ex.what() << endl;
        return "[ERROR]";
    }
}

string MatReader::getDiffuseLocation()
{
    string line;
    vector<string> seglist;
    try
    {
        if (ofile == false)
            throw exception("file not open");
        while (getline(text, line, '"'))
        {
            seglist.push_back(line);
        }
        for (uint16_t i{}; i < seglist.size(); i++)
        {
            if (seglist[i - 1] == " File=")
            {
                PhysicsMaterial = seglist[i];
                return seglist[i];
            }
        }
    }
    catch (const std::exception& ex)
    {
        cout << "[ERROR]: " << ex.what() << endl;
        return "[ERROR]";
    }
}

The methods "getPhysMaterial()" and "getDiffuseLocation()" works separately without any problems, but if they are executed sequentially, they give an error or are not executed at all. Thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So first you need to correct the issue with your out-of-range array access. The next issue you have is you need to reset the position of the stream in order to re-read it from the beginning.

Here is an example of how you can do that.

std::stringstream ss{ };
ss << "This is line one
"
   << "This is line two
"
   << "This is line three
"
   << "This is line four
";

// Points to the start of the stringstream.
// You can store this as a member of your class.
const std::stringstream::pos_type start{ ss.tellg( ) };

std::string line{ };
while ( std::getline( ss, line ) )
    std::cout << line << '
'; 

// Reset to the start of the stringstream.
ss.clear( );
ss.seekg( start );

while ( std::getline( ss, line ) )
    std::cout << line << '
'; 

Another issue I noticed is that you're checking for eof in the loop condition. Don't do that.. Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())) considered wrong?

Do something like this instead:

std::stringstream ss{ };
if ( std::ifstream file{ "/Path/To/MyFile.txt" } )
{
    std::string input{ };
    while ( std::getline( file, input ) )
        ss << input << '
';
}
else 
{
    std::cerr << "Failed to open file
";
    return 1;
}

std::string line{ };
while ( std::getline( ss, line ) )
    std::cout << line << '
'; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...