Using F stream to read first few lines of a text file

Hi,

I am just learning about using fstream, I have succesfully opened a text file, the next part of what I want to do is first of all only use the first 4 lines of text from it, but then search for certain values within those 4 lines. Example... Lets pretend below is a text file:

1.Random irrelevent text here $500.22+$200.33 More random irrelevent text
2.Random text here 50-people more random text
3.user ben
4.user rando
5.werwerewrewr
6.werewrwerewrewr
7.ewrewrwerwerwerwer
8.hundreds more lines of irrelevent text.

I numbered each line just for the sake of this post so the text file has hundreds and hundreds of lines of codes but I am only interested in the first 4 lines, how do I set fstream to only scan that for effeciency?

Second of all from line 1. I only want the values $500.22 and the value $200.33 (stored seperatley in variables)

Next I want line 2 to do a check that it contains the string "50-people"

And lastly I just want check line 3 or 4 contains "user ben"

How would I go about this using fstream?

Cheers,

Ben.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
	fstream handHistory;
	handHistory.open ("test.txt" ,ios::in) ;
	
	if (! handHistory.is_open () )
	{
		cout << "unable to open file" << endl;
	}
	

	system("PAUSE");

}
Last edited on
closed account (Dy7SLyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
	ifstream File("MyFile.txt");
	vector<string> Line(4);
	int Counter = 0;
	
	while(getline(File, Line[counter]) && Counter++ != 4);
}
Thanks, thats cool looks easy enough but I havent done any work with vectors yet only arrays :(.

How would I then work with each line? for example line one I only want the two $xxx.xx values, how do I write a statement to collect that information and store it in a variable? and to access the vector should be easy enough for example the comparison would be like

1
2
3
4
if ( line (3) == "user ben" || line (4) == "user ben" )
{
   conditions go here...
}


sory if that looks horribly wrong, just guess :)
Last edited on
closed account (Dy7SLyTq)
imagine a vector as a better array how you use it:

//upon declaring it
vector<type> MyVector (how much space it takes);
//then just treat it like a normal array
//if you run out of space:
MyVector.resize(how much more space i need);

then for the line just parse it until you hit the token you want
Weird, first time using vectors, any idea? VS 2012 (console) error msg:

http://oi41.tinypic.com/10f86x0.jpg

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
31
32
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main ()
{
	ifstream handHistory;
	handHistory.open ("HH.txt" ,ios::in) ;
	
	if (! handHistory.is_open () )
	{
		cout << "unable to open file" << endl;
	}
	
	vector<string> Line(4);
	int Counter = 0;
	
	while (getline(handHistory, Line[Counter]) && Counter++ != 4);
	

	cout << "line 1 says " << //syntax for lines here ;

	


	system("PAUSE");

}
closed account (Dy7SLyTq)
cout << "line 1 says " << Line[0] << endl;
Yeh ok I made the mistake of using Line(3) on the cout instead of brackets [3] , any idea on the error msg?
Last edited on
closed account (Dy7SLyTq)
did you write line instead of Line?
I just commented out the cout completley and still get that error message , all Lines have caps L
Last edited on
closed account (Dy7SLyTq)
ill have to take a look later. ill write something that might work
Ok I have managed to filter down to the first 4 lines now using this.

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
31
32
33
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
	ifstream handHistory;
	handHistory.open ("HH.txt") ;
	
	if (! handHistory.is_open () )
	{
		cout << "unable to open file" << endl;
		return 0;
	}

	string word;
	handHistory >> word;

	for (int i = 0; i < 4; i++)
	{
		getline (handHistory, word);
		cout << word << endl;
		
	}

	system("PAUSE");
	return 0;

}



I can get each individual line just using

1
2
3
4
5
 //Display an individual line
if (i = 2) 
{ 
   cout << word
}



How do I search for the exact phrases I am looking for within each line?

Last edited on
Any ideas anyone
> How do I search for the exact phrases I am looking for within each line?

http://www.cplusplus.com/reference/string/string/find/
Hmm just been playing around with that , I cant seem to get it to work im using the following.

1
2
3
4
5
6
7
8
9
10
11
12
13
	string word;
	handHistory >> word;

	//Loops and prints each line of HH x 4
	for (int i = 0; i < 4; i++)
	{
		getline (handHistory, word);
		cout << word << endl ;
		if (i == 0)
		{
			cout << word.find("$") ;
		}
	}


so on line one find $ and cout where the char starts, it keeps reporting it incorrectly I assume its ignoring int's or something , any small example u can provide?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

int main()
{
    {
        const std::string str = "Random irrelevent text here $500.22+$200.33 More random $irrelevent text$" ;
        std::cout << str << "\n    character '$' found at positions: " ;
        std::string::size_type pos = str.find( '$' ) ;
        while( pos != std::string::npos )
        {
            std::cout << pos << ' ' ;
            pos = str.find( '$', pos+1 ) ;
        }
    }

    {
        const std::string str = "\n\nRandom text here 50-people more random text" ;
        std::cout << str << "\n    substring '50-people' found starting at position: "
                  << str.find( "50-people" ) << '\n' ;
        if( str.find( '$' ) == std::string::npos ) std::cout << "    character '$' was not found\n" ;
    }
}

http://ideone.com/nmyxyb
Thanks alot! Although I realised looking through my code I was pulling the data for the first 4 lines really horribly so I decided to store them in an array after each character return which works great.

1
2
3
4
5
6
7
8
9
10
11
12
13
//Store Each line
string Line[4];
	
//Loop each line of text and then store it in the Line Array
for (int i = 0; i < 4; i++)
{
	getline (handHistory, Line[i]);	
}

//Find $ symbol to capture the start of BI
//(Now need code to find how to grab 
//everything between $ and end with the "+" symbol)
cout << Line[0].find ("$") ;


The find function is also working perfectly, so how would I code it to say something like this.

"This text here is my example of Line[0] $100.22+$2.44 and now i'm finishing my example"

When I use the find function it detects where the first instance of the $ symbol is perfectly and also detects where the + symbol is perfectly, how could I capture data between those symbols so in this case $100.22+ , I would just want to capture 100.22 and save that as and integer named BI.

Rather than just finding 100.22 it needs to be between the $ and the + symbol because the number will be changing each time it is accessed so it may be 100.22 now but will be 194.56 another time.

Cheers,

Ben.

EDIT - This is actually gone fairly far off the orginal topic and I have acheived what I wanted to in this topic so ill open a new one , thanks for your input :)
Last edited on
Topic archived. No new replies allowed.