Ok here is what I am trying to do at work as it's slower than a turtle in a race right now.

I have a log of text guessing about 40 lines total. the last three sentences are what I am after .. being 38,39,40.
I need to be able to read each sentence in as a complete string so that I can basically get my total 40 sentences stored in an array of some type (string array prefferably) so I can simply output to the screen just those last 3 sentences.

I appreciate the help, Im getting back into coding now that I have a job that lets me use some of it so any help would be appreciated.

I understand getline only takes in chars, and have been able to read in a whole file using it, but I am wanting to be able to have control over each sentence itself.

Recommended Answers

All 9 Replies

If you use std::string and std::ifstream, then you can loop through the file using getline(). Store each string in a std::vector array. The code is very simple, like this: If you only want certain line numbers then just count them inside the loop.

#include <vector>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    vector<string> array;
    string line;
    ifstream in("filename");
    while( getline(in,line))
    {
           array.push_back(line);
    }
}

>>but I am wanting to be able to have control over each sentence itself.
Do you mean each line? getline() knows nothing about sentences. Is there something unique that separates one sentence from another, such as a blank line?

THe only thing that seperates each sentence would be a new line.
Example:
File Name\location: c:\\text.txt
Contents of c:\\text.txt

My name is Cody
My Last name is Oebel
I am getting back into c++
and laughing at how I am slowly learning
this all over again

Your Vector idea seem's great .. I remember reading about vectors long ago, but if theres an easier way please let me know. So the protocol of each sentence is no period, simply it newlines.
So say I wanted to read all of the above in bold, but only wanted to output the last two lines:


and laughing at how I am slowly learning
this all over again

is what I am trying to achieve.

THis is a small example, as I am really trying to read in a log of about 100 or so lines, and need the last three to output to something else.

Thanks AD .. I will be waiting for your reply if you think of something from this that would further help. :)

Note: I thought getline can only take in a character type?

>>THe only thing that seperates each sentence would be a new line.
Those are not sentences. Those are just lines, and getline() is what you want to use.

using a vector is the easiest way to do it when you don't know how many lines there are. If you know you only want two lines then just create an array of 2 strings.

There are lots of ways to do your program. I gave you a start and will leave the rest for you to do.

Ok Im having a small problem with the below code.
I wanted to get basic with the vectors before diong what I am trying to do with the bigger picture of things. Only it's not working like I thought it would. Can you tell me why the cout<< strings; is not outputting from 0 - 3 even though I am incrimenting i ?

using namespace std;
vector<string> strings;
bool done = false;
int main(int argc, char *argv[])
{
  
string sentence;
sentence = "This is sentence Two\n";    
strings.push_back("This is sentence One\n");
strings.push_back(sentence);
strings.push_back("This is sentence Three\n");
while(!done)
{
for(int k = 0; k < 3; k++)
{

int i = 0;


cout<< strings[i];
i++;
}

}


system("pause");
return 0;
}

line 1: you forgot to add the includes

you can delete lines 17 and 21, and just use variable k in line 20.

Here is a simplified version of your program

#include <string>
#include <vector>
#include <iostream>
using namespace std;
vector<string> strings;
bool done = false;

int main()
{ 
string sentence;
sentence = "This is sentence Two\n";    
strings.push_back("This is sentence One\n");
strings.push_back(sentence);
strings.push_back("This is sentence Three\n");
for(int k = 0; k < 3; k++)
{
    cout<< strings[k] << "\n";
}
system("pause");
return 0;
}

Ive searched and searched.. and cannot find something that I can tie this into to accomplish what I am wanting to do.
I have a couple of idea's, but how can I read stuff into a vector and have it incriment for a new sentence storage.

*Not giving up on searching, but just havent found anything usefull*

There are lots of examples right here at DaniWeb. Just search for ifstream and getline()

#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
   vector<string> strings
   string line;
   ifstream in("filename");
   while( getline(in, line) )
        string.push_back(line);
}

THANKSSSSSSSSS AD I got it almost figured out with your help man :)
Thanks for putting scraps on the table and making me think about it lol
the whole while(getline) idea was masterfull.. BELOW IS A POST WITH MY FINISHED RESULTS.
YOU SOLVED THIS and helped me to better understand.

WOOOOOOOOOHOOOOOOOOOO
THANK YOU AD... YOU ARE THE MASTER !
SHould be more like MASTER DRAGON or something heh...

heres my updated code

#include <iostream>
#include <string>
#include <vector>
#include <fstream>


using namespace std;
vector<string> str;
string line;


int main(int argc, char *argv[])

{
ifstream in;
in.open("c:\\test.txt");
while(getline(in, line))
{
str.push_back(line);
}
for(int i=0;i<str.size();i++)//Edited.. and added in the str.size() to be compared against int i.
{
cout<< str[i] <<"\n";
};
    
   
    

system("pause");
return 0;
}

ok in my c:\test.txt is simply these lines.
file: c:\test.txt & its contents

Line one is being read in and stored.
line two is being read in and store.
line three is being read in and stored.
line four is being read in and store.
line 5.
line 6 is being stored


AD you fed me perfectly :) I was getting a little frustrated , did my research and needed a wing. WOOOHOOO. Now all I have to figure out is how to itterate the total number of things stored in a vector so I can get my total and further manipulate. I took what hint you gave me, and with some of the stuff you gave me earlier, and it's working nice :) THANKS MAN... SERIOUSLY !

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.