Unknown C ++ bug unknown only for Linux

As the title says, I have a bug in C ++ that only occurs on Linux (Ubuntu, C ++ 11). Now the code is slightly modified for debugging, so it should print the first ten words compared to "myword".

//Converts String to lower case
string &strToLower(string& s) {
    for(char& c: s) {
        if(c > 64 && c < 64 + 27) c += 32;
    }
    return s;
}

//Main Method
int main(int argc, char** args) {
    const string libfile = "english-wordlist.txt";
    const string filename = "myfile.txt";
    const string outfile = "myfile.out";

    string myword = "word"; //Word to be compared

    //Intended for future use
    ifstream f(filename);
    vector<string> words;
    for(string line; getline(f, line); ) {
        for(string& s: split(line)) {
            words.push_back(strToLower(s));
        }
    }
    f.close();

    //Read the other file
    f.open(libfile);
    vector<unsigned char> keys;
    int ln = 0;
    for(string line; ln < 10 && getline(f, line); ln++) {
        //Guessing error occurs near this line
        cout << "Testing " << line << " against " << myword << endl;
        if(strToLower(line) == myword) {
            cout << "Found word at " << ln << endl;
        }
    }
    f.close();

    cout << endl << "Finished " << endl;
    //while(getchar() != 'e'); //Used for Window OS
    return 0;
}

However, the conclusion:

 against word
 against word
 against word
 against wordg
 against word
 against wordark
 against wordarks
 against wordolf
 against word
 against word

Finished 

Does anyone know why this is happening?

+4
source share
1 answer

, , , ('\r') ('\n') - , , Windows. Windows IO '\r' ( ), Windows . , '\r' . , '\r' , .

, '\r' (, char '\r' '\0', ), , sed 's/\r//g' dos2unix.

+2

All Articles