0

Here is how my file data is arranged

10 12 19 21 3
11 18 25 2 9

1 3 1
0 5 0
2 1 2

when i use getline() and istringstream to line by line strip the file, i am concerning to detect the blank line in between these two data blocks. I need to detect it not to skip it.

so i wrote

while(getline(fp1,line)){
 if(line.empty()){
 cout<<"empty line"<<endl;
}

it does not work. And i think maybe the line is empty but contains with white space so I wrote

    while(getline(fp1,line)){
 if(line == "\n"){
 cout<<"empty line"<<endl;
}

not working. I even used line.find_first_not_of(' ') == std::string::npos as the condition, still no luck. Then i am thinking to print this blank space out to see what is in it.I printed all the length of my line, and i found the empty line has size 1. so then i wrote

if(line.length() == 1){
  cout<<hex<<  line;
  } 

i got a blank line back without anything.

I am confused. What am i suppose to do to detect this blank line? Please help!

melpomene
  • 79,257
  • 6
  • 70
  • 127
user3431800
  • 181
  • 2
  • 14
  • 2
    `empty` is equal to 0 size so the white line is not empty. – Raindrop7 Feb 11 '17 at 22:42
  • obviously i found it is not empty. if you look over here http://stackoverflow.com/questions/9235296/how-to-detect-empty-lines-while-reading-from-istream-object-in-c, the answer is accepted. – user3431800 Feb 11 '17 at 22:46
  • Print the code values of each character in the line. It might be something non-printable – Kerrek SB Feb 11 '17 at 22:47
  • I'm not sure what you expect that `< – melpomene Feb 11 '17 at 22:48
  • Try `cout << (line[0] + 0) << '\n'`. My money is on `13`. – melpomene Feb 11 '17 at 22:54
  • Define, as precisely as you can, what you mean by a "blank line", and then write code to detect that. If you mean a line that contains no printable characters, then write code to check if there are any printable characters. – David Schwartz Feb 11 '17 at 22:58

2 Answers2

1

You can make a bool variable isBlanksetting it to true and inside the while loop after every line input you iterate over the line whether it is a blank or not:

std::ifstream in("test.txt");
std::string sLine;
bool isBlank = true;

while(std::getline(in, sLine)){
    isBlank = true;
    for(int i(0); i < sLine.length(); i++){
        if(!isspace(sLine[i])){
            isBlank = false;
            break;
        }
    }
    if(isBlank)
        std::cout << "Blank Line" << std:: endl;
    else
        std::cout << sLine << std::endl;
}

The output:

0 12 19 21 3
11 18 25 2 9
Blank Line
1 3 1
0 5 0
2 1 2
Raindrop7
  • 3,805
  • 3
  • 14
  • 27
  • isspace only checks for white space. I printed out its ascii code, only a carriage return is contained in that line. I dont know why... can a new line contain white space only or it can also be a CR or combined with both? – user3431800 Feb 11 '17 at 22:59
  • 1
    @user3431800 Carriage return is one of the whitespace characters. What's unclear? – melpomene Feb 11 '17 at 23:01
  • Erm... usually blank line means that it contains zero characters. You shouldn't call any whitespace-only line the blank one. – Paweł Stankowski Feb 11 '17 at 23:29
0

I think the additional character may be carriage return (\r) or another whitespace character. Note that std::hex does not affect strings or chars. To check what it is try:

cout<<hex<<  (int)line[0];
melpomene
  • 79,257
  • 6
  • 70
  • 127
  • thanks. i checked it that how i found its a carriage return. I see all people are only checking for space characters. many posts here including the one i mentioned under my original post plus this one http://stackoverflow.com/questions/6444842/efficient-way-to-check-if-stdstring-has-only-spaces. I am still confused with what a blank linecontains. Does it only contain space or a carriage return or maybe both? – user3431800 Feb 11 '17 at 23:14
  • Carriage return is additional character added before each newline (\n) in Windows-compatible editors. Just treat the line with single \r character the same as empty line and you should be ok. Line containing single space is not any special and I wouldn't call it 'blank'. BTW: windows treats carriage return differently when you open file in binary mode vs text mode. Maybe that's why you are confused. Check: http://stackoverflow.com/questions/229924/difference-between-files-writen-in-binary-and-text-mode – Paweł Stankowski Feb 11 '17 at 23:22
  • yes, that's what i am asking, something about how the system treat an unprintable line. Thanks – user3431800 Feb 11 '17 at 23:37
  • So this should be interesting for you: https://en.m.wikipedia.org/wiki/Newline%23In_programming_languages – Paweł Stankowski Feb 11 '17 at 23:44