How to get the last but not empty line in a txt file

I want to get the last but not empty line in a txt file.

This is my code:

string line1, line2;
ifstream myfile(argv[1]);
if(myfile.is_open())
{
    while( !myfile.eof() )
    {
        getline(myfile, line1);
        if( line1 != "" || line1 != "\t" || line1 != "\n" || !line1.empty() )
            line2 = line1;
    }
    myfile.close();
}
else
    cout << "Unable to open file";

The problem is that I cannot check for an empty string.

+3
source share
5 answers

It would not be enough to change your || to && to check if the string is empty. What if there are seven spaces, a tab character, 3 more spaces, and finally a new line? You cannot list all the ways to get only spaces in a string. Instead, check each character in the line to see if it is a space.

This code is_emptywill be false if any character without a space is found in the string.

bool is_empty = true;
for (int i = 0; i < line.size(); i++) {
    char ch = line[i];
    is_empty = is_empty && isspace(ch);
}

Complete solution:

#include <iostream>
#include <fstream>
#include <cctype>
#include <string>

using namespace std;

int main(int argc, char* argv[]) {
    string line;
    string last_line;

    ifstream myfile(argv[1]);
    if(myfile.is_open())
    {
        while( getline(myfile, line) ) {
            bool is_empty = true;
            for (int i = 0; i < line.size(); i++) {
                char ch = line[i];
                is_empty = is_empty && isspace(ch);
            }
            if (!is_empty) {
                last_line = line;
            }
        }
        myfile.close();
        cout << "Last line: " << last_line << endl;
    }
    else {
        cout << "Unable to open file";
    }   

    return 0;
}
+3
source

, . : while( !myfile.eof() ), , , . getline , :

while (getline(myfile, line1)) // ...

, :

    if( line1 != "" || line1 != "\t" || line1 != "\n" || !line1.empty() )
        line2 = line1;

... . , && || . , , , line1 , (.. - , ). !line1.empty(), line1 != "" .

+8

? , , . , .

int main(int argc, char **argv)
{
   std::cout<<"Opening "<<fn<<std::endl;
   std::fstream fin(fn.c_str(), std::ios_base::in);
   //go to end
   fin.seekg(0, std::ios_base::end);
   int currpos = fin.tellg();
   //go to 1 before end of file
   if(currpos > 0)
   {
       //collect the chars here...
       std::vector<char> chars;
       fin.seekg(currpos - 1);
       currpos = fin.tellg();
       while(currpos > 0)
       {
           char c = fin.get();
           if(!fin.good())
           {
               break;
           }
           chars.push_back(c);
           currpos -= 1;
           fin.seekg(currpos);
       }
       //do whatever u want with chars...
       //this is the reversed order
       for(std::vector<char>::size_type i = 0; i < chars.size(); ++i)
       {
           std::cout<<chars[i];
       }
       //this is the forward order...
       for(std::vector<char>::size_type i = chars.size(); i != 0; --i)
       {
           std::cout<<chars[i-1];
       }
   }
   return 0;
}
+4

, :
, myfile >> std::ws, std::getline(). .

!line1.empty(). , , .

+2

get_last_line , . , instream get_last_line . 1 char . reset, ios_base:: end, ()

std::string& get_last_line(
        std::istream& in_stream, 
        std::string& output = std::string(), 
        std::ios_base::seekdir reset = std::ios_base::cur)
{
    output.clear();
    std::streambuf& buf = *in_stream.rdbuf();
    bool text_found = false;

    while(buf.pubseekoff(-1, std::ios_base::cur) >= 0)
    {
        char c = buf.sgetc();
        if(!isspace(c))
            text_found = true;
        if(text_found)
        {
            if(c == '\n' || c == -1)
                break;
            output.insert(0, sizeof c, c);
        }
    }

    buf.pubseekoff(0, reset);
    return output;
}

std::string& get_last_line(
        const std::string& file_name, 
        std::string& output = std::string())
{
    std::ifstream file_in(
        file_name.c_str(), 
        std::ios_base::in | std::ios_base::ate);
    if(!file_in.is_open())
    {
        output.clear();
        return output;
    }
    get_last_line(file_in, output);
    file_in.close();
    return output;
}
+1
source

Source: https://habr.com/ru/post/1755657/


All Articles