user3247278
user3247278

Reputation: 179

How can identify a blank line in C++?

I'm reading in information from a file. I need a counter that counts how many text filled lines there are. I need that counter to stop if there is any blank line (even if there are text filled lines after that blank line).

How would I do this? Because I'm not exactly sure how to identify a blank line to stop the counter there.

Upvotes: 0

Views: 17442

Answers (6)

Anirban Pal
Anirban Pal

Reputation: 529

Simply check the string length and use a line counter. When the string length is zero (i.e., the string is blank) print the line counter. Sample code is provided for your reference:

// Reading a text file

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
    string line;
    ifstream myfile ("example.txt");
    int i = 0;
    if (myfile.is_open())
    {
        while (getline (myfile, line))
        {
            i++;
            // cout << line << '\n';
            if (line.length() == 0)
                break;
        }
        cout << "blank line found at line no. " << i << "\n";
        myfile.close();
    }
    else
        cout << "Unable to open file";

     return 0;
}

Upvotes: 0

J. A. Streich
J. A. Streich

Reputation: 1702

No error checking, no protection, just a simple example... It is not tested, but you get the gist.

#incldue <iostream>
#include <string>

using namespace std;

int main()
{
  string str = "";
  int blank = 0, text = 0;
  ifstream myfile;
  myfile.open("pathToFile");
  while(getline(myfile,str))
  {
    if(str == "")
    {
      ++blank;
    }
    else
    {
      ++text;
    }
  }
  cout << "Blank: " << blank << "\t" << "With text: " << text;
  return 0;
}

Upvotes: 0

quetzalcoatl
quetzalcoatl

Reputation: 33506

In a loop, read all lines, one-by-one, into a single string variable. You can use a std::getline function for that.

Each time after reading a line into that variable, check its length. If it's zero, then the line is empty, and in that case break the loop.

However, checking for empty lines like is not always really right thing. If you are sure that the lines will be empty, then it's OK. But if your "empty" lines can contain whitespaces,

123 2 312 3213
12 3123 123
              // <---  Here are SPACEs. Is it "empty"?
123 123 213
123 21312 3

then you might need to not check for "zero-length", but rather whether "all characters are whitespaces".

Upvotes: 0

AliciaBytes
AliciaBytes

Reputation: 7429

I'd suggest using std::getline for it:

#include <string>
#include <iostream>

int main()
{
    unsigned counter = 0;
    std::string line;

    while (std::getline(std::cin, line) && line != "")
        ++counter;

    std::cout << counter << std::endl;
    return 0;
}

Since @Edward made a comment about handling whitespace and it might be important. When lines with only whitespaces are considered as "empty lines" too I'd suggest changing it to:

#include <string>
#include <iostream>
#include <algorithm>
#include <cctype>

int main()
{
    unsigned counter = 0;
    std::string line;

    while (std::getline(std::cin, line) &&
           std::find_if_not( line.begin(), line.end(), std::isspace != line.end()) {
        ++counter;
    }

    std::cout << counter << std::endl;
    return 0;
}

It's quite verbose, but the advantage is that it uses std::isspace to handle all different kind of spaces (e.g. ' ', '\t', '\v', etc...) and you don't have to worry if you handle them correctly.

Upvotes: 2

UldisK
UldisK

Reputation: 1639

If you are using std::getline then you can just detect an empty line by checking if the std::string you have just read is empty.

std::ifstream stream;
stream.open("file.txt");
std::string text;
while(std::getline(stream,text))
    if(!text.size())
        std::cout << "empty" << std::endl;

Upvotes: 4

Pranit Kothari
Pranit Kothari

Reputation: 9841

In C++ 11 you can use,

std::isblank

Upvotes: 0

Related Questions