Thread: Reading a line including blanks

  1. #1
    Registered User
    Join Date
    Nov 2002

    Question Reading a line including blanks

    I'm trying to read a whole line from a file including the spaces in between the data but I can't get it to work. Also, I'm trying to read the blank rows in between the lines, and it's not working neither.

    For example, for a data file such as:
    \\begin file
    Hello, how are you doing?

    I hope that you are fine.
    \\end file
    I'm trying to read in the entire first line including the spaces in between the words, then I'm trying to return the value of the number of lines in the file which is three for this example, and the number of blank lines which is one. How would I do that?? When I use char string[80], for example, string only reads up to the first blank, in this case it only reads each word separate and returns a count of words instead of a count of lines. I know that getline can read an entire line, but I don't know how to implement it in a program to read the lines in a file.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Function getline() will read a whole line.
    char line[100];
    ifstream in(filename);

  3. #3
    Registered User
    Join Date
    Feb 2002


    #include <fstream>
    int main()
        ifstream file;
        string input;
        int count = 0;
        int spacecount = 0;
             if(input == "")
       return 0;
    i believe something like above is what u want.. btw the above code isn't finished.. i just wrote some quick crap... to help u in the right direction..... input == "" may not work either.. do not know what is stored in the string if a blank line is encountered.. never tried it...
    Last edited by tegwin; 11-13-2002 at 04:12 PM.

  4. #4
    Registered User
    Join Date
    Nov 2001
    You may use the string object with getline as the others have mentioned.

    // This code will read up to "\n."
    std::getline(cin, theString);


  5. #5
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002

    How bout this...?

    #include <iomanip> //for noskipws

    cin >> noskipws >> your_statement >> endl;

    noskipws = dont skip white space on input

    skipws = skip white space on input

    I hop this helps ya!
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading a string of hex values from command line
    By peeweearies in forum C Programming
    Replies: 7
    Last Post: 02-24-2009, 02:25 AM
  2. Replies: 1
    Last Post: 05-20-2006, 11:17 PM
  3. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  4. Reading a user specificed line from a file
    By hslee16 in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2004, 06:58 PM
  5. Reading a line from mapped memory
    By Zughiaq in forum C Programming
    Replies: 7
    Last Post: 05-02-2003, 11:01 AM