Question About Blank Lines in Text Files

This is a discussion on Question About Blank Lines in Text Files within the C++ Programming forums, part of the General Programming Boards category; Hello, I was just curious how blank lines are handled when using getline? Say I have a text file as ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Question About Blank Lines in Text Files

    Hello,

    I was just curious how blank lines are handled when using getline? Say I have a text file as follows:

    Line of text

    Line of text again
    And I have some simple code like this:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
    
            string line;
    
            while (getline(cin,line) )
                    if (line == blank) cout << "A blank line!" << endl;
    
    }
    What would I put in the "blank" spot to test if the line that was retrieved was blank? Thanks.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,707
    Code:
    if(str == '\n') {
     // it's a blank
     }
    Code:
    #include <cmath>
    #include <complex>
    bool flip(bool value)
    {
           return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0)*(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    I tried putting that in my code and I get a compile error. I then tried putting double quotes around it, but I received no output. Here's my code with the new addition:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    int main() {
    
            string line;
    
            while (getline(cin,line) )
                    if (line == '\n') cout << "A blank line!" << endl;
    
    }

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Use double quotes around the \n. A std::string can't be compared to a single character.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    I did that, but it doesn't seem to work. When it encounters the blank line, it should print "A blank line!" but instead it prints nothing at all. Is there something else that needs to be done?

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    On windows machines, a blank line is probably represented by "\r\n", not "\n".

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    But I'm running this program on UNIX. Sorry, I should have mentioned that before.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Zildjian
    But I'm running this program on UNIX. Sorry, I should have mentioned that before.

    Code:
      if (str = "") {
          cout << "A blank line!" << endl;
    
      }
    Regards,

    Dave

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    '\n' is the default delimiter for 'getline()' and any delimiter will be extracted but not stored. So probably check if the string is empty.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    if (str == "") {

  11. #11
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by Dave Evans
    Code:
      if (str == "") {
          cout << "A blank line!" << endl;
    
      }
    Regards,

    Dave
    fixed

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Even better:
    Code:
    if (line.empty())

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LaTeX Question: Displaying Text and Images
    By Reisswolf in forum Tech Board
    Replies: 0
    Last Post: 06-30-2006, 08:01 AM
  2. Replies: 15
    Last Post: 10-31-2005, 07:29 AM
  3. Folders and text files
    By tim545666 in forum C++ Programming
    Replies: 11
    Last Post: 02-17-2002, 03:15 PM
  4. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 05:21 PM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21