Is there a good way to determine the end of a a line in a text file? I need to find of end of a line in text because all text lines are of different length. I also have to store each word (separated by spaces) into a vector.

abcd:  12345  aaa
abcd:  23456  aaa bbb
abcd:  34567  aaa bbb ccc 
abcd:  45678  aaa bbb ccc ddd

I want to separate this into the two following vectors:

vector<string> words:
[abdc:,12345,aaa,abcd:,23456,aaa,bbb,abcd:,34567,aaa,bbb,ccc,abcd:,45678,aaa,bbb,ccc,ddd]
vector<string> length_of_sentances:
[3,4,5,6]

If I remember correctly, you should be able to read in line by line from a text file. You should be able to count the elements right where you parse the line.

To answer your question, you could just determine the length while you parse it. Just count the length of elements while you are inserting it. You should know that each line would start with a string that always ends with ':'.

I would suggest you to change the way you implement your data structure. This could cause more headache later on because you are trying to operate everything in string. If you use struct instead of string in your vector, it could help you from encountering troubles in the future.

Fortunately in this case all I'm doing is a text conversion for another program to be able to read my instructions correctly. After the conversion to the correct format I can dust my hands of the content.

Is there a good way to determine the end of a a line in a text file?

getline(istream,string) method should work fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.