samedi 28 novembre 2015

Detecting a blank line in C++ from reading a file

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.

Aucun commentaire:

Enregistrer un commentaire