ofstream problem

Hello everyone,i have this following code where i read from a txt file and i pass each of the 3 first lines into three integers.The fourth line is txt,i try to pass the fourth into i string but i can't.Then i try it to pass in a array but it reads until the first space.
What should i do i order to read the hole fourth line?

Here is how my txt look like:
1
2
3
Hello World

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<fstream>
#include<iostream>
using namespace std;

int main()
{
	ifstream inStream,inStream2;
	ofstream outStream;

	inStream.open("infile.txt");
	outStream.open("outline.txt");

	int first,second,third;
	char str[20]; //Here is my concern 

	inStream>>first>>second>>third>>str;


	

	outStream<<"The sum of the numbers \n"
		      <<"in infile.txt \n"
			  <<"is:"<<(first+second+third)
			  <<" and the text:"
			  <<str<<endl;

	inStream.close();
	outStream.close();
	
	cin.get();
	return 0;
}
Last edited on
Try this:

1
2
3
4
5
6
7
8
9
10
11
//...
    int first,second,third;
    string str;

    inStream>>first>>second>>third;
    
    inStream.get(); //get rid of the
                    //newline character
    
    getline(inStream,str);
//... 

Useful link -> http://cplusplus.com/reference/string/getline/
Last edited on
Thanks re patriwti :)
Last edited on
Topic archived. No new replies allowed.