Text file not read properly

Oct 31, 2014 at 11:03pm
I am reading string from a line in a text file and for some reason the the code will not read the whole text file. It reads to some random point and then stops and leaves out several words from a line or a few lines. Here is my code.
1
2
3
4
5
6
7
8
9
10
string total;
while(file >> word){
        if(total.size() <= 40){
            total +=  ' ' + word;
        }
        else{
            my_vector.push_back(total);
            total.clear();
        }
    }
Oct 31, 2014 at 11:54pm
Can you post a sample of the text file? and word's type.

Aceix.
Nov 1, 2014 at 12:10am
Here is a sample file that I have:

Informed-Sport is a quality assurance programme for
sports nutrition products, suppliers to the sports
nutrition industry, and supplement manufacturing
facilities.

The programme certifies that all nutritional supplements
and/or ingredients that bear the Informed-Sport logo have
been tested for banned substances by the world class sports
anti-doping lab, LGC. Athletes choosing to use supplements
can use the search function above to find products that have
been through this rigorous certification process.


My program reads only up to "through" and leaves out "this rigorous certification process."
Nov 1, 2014 at 12:25am
Your program acts as a buffer of 40 characters that empties it self to a vector ony when the buffer is full.

Maybe that is the reason why the last part of the file was left out.

your sample might not be divisible by 40.
Nov 1, 2014 at 12:47am
Ok so is there a way I can fix that. I want to be able to have the buffer hold any number of characters including 40.
Last edited on Nov 1, 2014 at 12:48am
Nov 1, 2014 at 12:52am
total can hold any length of string. I dont know why you have a vector.
1
2
while(file>>word)
  total += word + '  ';

Nov 1, 2014 at 2:42pm
Then empty the buffer into the vector right after the loop has ended.

Aceix.
Topic archived. No new replies allowed.