Measuring blank spaces from a text file

I have written the following code
On line number 19, Input_file>>line ;
this only take first word of file, how do I proceed??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include<iostream>
#include<locale>
#include<fstream>
#include<string>
using namespace std;
int main()
{
    ifstream Input_file;
    string line;
    int counter=0;
    char c;
    locale LOC;
    Input_file.open("example.txt");
    if(Input_file.fail())
    {
        cout<<"File is not open";
    }

Input_file>>line;
 
    for(string::size_type i=0;i<line.length();i++)
    {
        c=line[i];
        if(isspace(c,LOC))counter++;
    }
  
    cout<<counter;
    return 0;
}
Last edited on
See the example of using getline() to read from a file on this tutorial page:
http://www.cplusplus.com/doc/tutorial/files/
Thanks , it worked...

but if I want to measure black spaces from next line??

There is are several line inside that text file...

Do look at the description of skipws/noskipws from:
http://www.cplusplus.com/reference/istream/basic_istream/operator%3E%3E/


You could also read the file one character at a time.
but if I want to measure black spaces from next line??

There is are several line inside that text file..


The example which I linked previously does read all the lines from the file, using a while loop.

1
2
3
4
    while ( getline (myfile,line) )
    {
      cout << line << '\n';
    }


What you could do, inside the loop where it says
 
      cout << line << '\n';

is to add your code to process the string line.
ohhh thanks , it worked...

:) :) :)

Topic archived. No new replies allowed.