checking for blank line while reading file

Hello,

I need to check for the presence of blank lines in a text file. My code reads like this:

string currentLine;

while(getline(inFile, currentLine))
{
if (currentLine != "blank line")
{
cout << currentLine << endl;
}
}

I need to know what to put in place of "blank line" to get the program to work. Thank you for answering this very basic question.

Comments

  • : Hello,
    :
    : I need to check for the presence of blank lines in a text file. My code reads like this:
    :
    : string currentLine;
    :
    : while(getline(inFile, currentLine))
    : {
    : if (currentLine != "blank line")
    : {
    : cout << currentLine << endl;
    : }
    : }
    :
    : I need to know what to put in place of "blank line" to get the program to work. Thank you for answering this very basic question.
    :

    what's wrong with "" ?

  • : : Hello,
    : :
    : : I need to check for the presence of blank lines in a text file. My code reads like this:
    : :
    : : string currentLine;
    : :
    : : while(getline(inFile, currentLine))
    : : {
    : : if (currentLine != "blank line")
    : : {
    : : cout << currentLine << endl;
    : : }
    : : }
    : :
    : : I need to know what to put in place of "blank line" to get the program to work. Thank you for answering this very basic question.
    : :
    :
    : what's wrong with "" ?
    :
    If you consider a line consisting solely of spaces, newlines and other control characters, a blank line, use the following C-style function:
    [code]
    #include <ctype.h>

    bool IsBlank(char* line)
    {
    int len;;
    int i;

    len = strlen(line);
    for (i=0; i<len; i++)
    if (isspace(line[i]) == FALSE)
    return FALSE;

    return TRUE;
    }
    [/code]

    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • c++ example
    [code]
    int main()
    {

    string name;
    cout << "Enter something ...";
    getline(cin,name);
    string::size_type n = name.find_first_not_of("
    ");
    if(n == string::npos)
    cout << "blank line
    ";
    else
    cout << "not a blank line
    ";
    return 0;
    }
    [/code]
  • : c++ example
    : [code]
    : int main()
    : {
    :
    : string name;
    : cout << "Enter something ...";
    : getline(cin,name);
    : string::size_type n = name.find_first_not_of("
    ");
    : if(n == string::npos)
    : cout << "blank line
    ";
    : else
    : cout << "not a blank line
    ";
    : return 0;
    : }
    : [/code]

    You're missing some characters here. The crt isspace function checks for 0x09, 0x0a, 0x0b, 0x0c, 0x0d and 0x20.
    There's no escape character for 0x0b (vertical tab). But, admitted, never seen that in a text file.

    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl



  • Thank you all for your help.

    I used code to check the length of string in the next line.

    If length of string is zero, it returns false. Works for now!
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories