简体   繁体   中英

How do i get std::getline past the first line

today i have a problem with my c++ code. The problematic bit is supposed to read a text file line by line and save specific lines in a vector.

OK, first, the code (part of it):

vector<string> write::get_lines() {
fstream in;
string path;
string line;
vector<string> lines;
if (elementType.substr(0,2).compare("od")==0) {
path = inFilePath + filename;
in.open(path.c_str(), ios::in);
std::getline(in,line);
getline(in,line);
    while (std::getline(in, line)) {
        if (line.substr(0, elementID.length()) == elementID) {
            lines.push_back(line);
        }
    }
} else {
    if (elementID.length() > 8){
        string typeInFile = elementType.substr(2);
        transform(typeInFile.begin(), typeInFile.end(), typeInFile.begin(), ::toupper);
        string elementID1 = elementID.substr(0,8);
        string elementID2 = elementID.substr(8);
    path = inFilePath + filename;
    in.open(path.c_str(), ios::in);
    while (getline(in, line)) {
        if (contains(line, typeInFile) && contains(line, elementID1) && contains(line, elementID2)) {
            lines.push_back(line);
        }
    }
    } else{
    string typeInFile = elementType.substr(2);
    transform(typeInFile.begin(), typeInFile.end(), typeInFile.begin(), ::toupper);
path = inFilePath + filename;
in.open(path.c_str(), ios::in);
while (getline(in, line)) {
    if (contains(line, typeInFile) && contains(line, elementID)) {
        lines.push_back(line);
    }
}
}
}
return lines;
}

The problem is the while loop, or rather the getline(in,line) . The filepath is correct and the file is opened. Unfortunately, the first line of the file is some sign i can't identify. It looks similar to this : ¬ But the short stroke is horizontal and the long stroke is vertical and downwards on the right side of the short one.

I'm guessing, that this is the problem. But i can't find a solution.

Now my question: How do i get std::getline() past this ominous sign.

edit: I posted the whole method. Maybe that helps.

OK, I'm a bloody idiot......

I had the file name wrong. It was spelled correctly, but i missed the point, that the file name has only capital letters.

Still, this article is pretty good and helped me figure it out....

how to for std::ifstream and std::getline

So, if anyone gets the same problem as me: CHECK YOUR SPELLING!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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