# Reading a line including blanks

This is a discussion on Reading a line including blanks within the C++ Programming forums, part of the General Programming Boards category; I'm trying to read a whole line from a file including the spaces in between the data but I can't ...

1. ## 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. Function getline() will read a whole line.
Code:
char line[100];

ifstream in(filename);
in.getline(line,100);

3. ## Ok

Code:
#include <fstream>

int main()
{
ifstream file;
string input;
int count = 0;
int spacecount = 0;

while(!eof)
{
file.getline(input,'\n');
if(input == "")
spacecount++;
count++;
}
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...

4. You may use the string object with getline as the others have mentioned.

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

Kuphryn

5. ## 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!
cj