Howdy, Stranger!

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

Categories

checking for blank line while reading file

serniter76serniter76 Member Posts: 2
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

  • stoberstober Member Posts: 9,765 ✭✭✭
    : 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 "" ?

  • tsagldtsagld Member Posts: 621
    : : 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


  • stoberstober Member Posts: 9,765 ✭✭✭
    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]
  • tsagldtsagld Member Posts: 621
    : 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


  • serniter76serniter76 Member Posts: 2

    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.