lundi 24 août 2020

When I read from a file I am getting control characters

I am trying to read from a file, but I keep getting control characters at the end of my string. I use the for loop to check for them and it prints out that there is one for every word, but only if I add the + 1 to temp.size(). I don't want to read them from the file. I am really confused why this is happening.

int main(){
    ifstream inFile;
    vector<string> vect;
    string temp = "";

    //Reading from a file line by line
    inFile.open("words.txt");
    if (inFile.is_open())
    {
        while (!inFile.eof())
        {
            getline(inFile, temp);
            vect.push_back(temp);
        }
    }
    inFile.close();

    //checking through each character of the string to see if it has a control character.
    for (int i = 0; i < vect.size(); i++)
    {
        temp = vect[i];
        for (int j = 0; j < temp.size() + 1; j++)
        {
            if (iscntrl(temp[j]))
            {
                cout << temp << " There is a space\n";
            }
        }
    }

    return 0;
}

.txt file image

Aucun commentaire:

Enregistrer un commentaire