Reputation: 149
I have a plain text file (.txt) with the following data;
data 5
data 7
data 8
I read this file in the following manner:
ifstream myReadFile;
myReadFile.open(fileName.c_str());
if (myReadFile.is_open() != true){
return -1;
}
string stri;
int i;
while (std::getline(myReadFile,stri)){
i++;
if(stri.find("data") != std::string::npos){ //extract data}
else if(stri.empty()){ cout << "Conditional statement true"; }
else { cout << "invalid keyword on line: " << i; }
}
I always receive the invalid keyword message and never does the conditional statement go through. I have tried if (stri == "") and (stri.compare("");
NOTE: It is safe to assume that the empty line contains NO WHITESPACE.
Upvotes: 2
Views: 236
Reputation: 206577
Here's one way to find the contents of the line that seems to be a problem.
else {
cout << "invalid keyword on line: " << i;
size_t size = stri.size();
for ( size_t i = 0; i < size; ++i )
{
// Print the ASCII code of the character.
cout << "ASCII code: " << (int)stri[i] << std::endl;
}
}
Find out what the ASCII codes represent from an ASCII code table. That will indicate what's being read into the line. It is most likely a carriage return, \r
, with ASCII code 13
.
Upvotes: 1