algorithm - How to determine if a line from a input file is the last line? c++ -


i have written program check balanced curly brackets in .cpp file. program works fine , finds syntax error, displays number of line problem , exits.

but have display different error message if error @ last line of input cpp file.

i have tried implement following way think wrong. doesn't work anyway :)

else                 {                     if(current == inputfile.eof()) //this tried                     {                         cout << "syntax error @ end of program.";                     }                     else                     {                         cout << "syntax error in line: " << current << "\n";                         errorfound == true;                     }                 } 

i did not give complete code because think simple if condition correct variable solve this. if need it, can post code later.

edit: larger piece of code given requested. counter int variable updated every line counter++.

for(int = 0; < line.length(); i++)         {             if (line[i] == '{')             {                 stack.push(current);             }             else if(line[i] == '}')             {                 if (!stack.isempty())                 {                     stack.pop(opening);                     cout << "code block: " << opening << " - " << current << "\n";                 }                 else                 {                     if(current == inputfile.eof())                     {                         cout << "syntax error @ end of program.";                     }                     else                     {                         cout << "syntax error in line: " << current << "\n";                         errorfound == true;                     }                 }             } 

this best solution think of. there better one.

std::ifstream input_file{ "file.txt }; std::vector<std::string> contents;  // fill vector file contents std::string cline; while (std::getline(input_file, cline))     contents.push_back(cline);  // loop (const auto& line : contents) {     //...     if (&line == &contents.back()) {         // @ end of file     } } 

you can use iterator version if don't pointer comparison :)


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -