help with file manipulators (read from/output to)

Hi, I need to change the following code so that char word is read from a file, and string str3 is output to a file.
to start do i use getline (inData,word);? and if so where? i tried putting it after i defined char word, but no joy, Also, i just added a text file in visual studio with the word 'fire' in line 1, is this correct? thanks in adcnaced.

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

int main() {

	ifstream inData;
	ofstream outData;
	inData.open("data.txt");
	outData.open("data.txt");




     char word[10] = "fire";
     word[0] = 'h';
     cout << word << endl;
     word[4] = 'd';
     cout << word << endl;
     word[5] = '\0';
     cout << word << endl;

     string str1("Now is");
     string str2 = " the winter";
	 cout << str1 << str2 << " of our discontent" << endl;
	 string str3 = str1 + str2 + "of our discontent";
	 cout << str3;
	 system ("pause");
return 0;
}
Last edited on
this is where I've got so far

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
34
35
36
37
38
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main() {

	
	ofstream outData;
	outData.open("data.txt");

	ifstream inData("data.txt");
	inData.open("data.txt");

    if(inData.is_open())
    {
        while(!inData.eof())
            cout << char(inData.get());
    }else cout << "Cannot find this file " << endl;



     char word[10] = "fire";
     word[0] = 'h';
     cout << word << endl;
     word[4] = 'd';
     cout << word << endl;
     word[5] = '\0';
     cout << word << endl;

     string str1("Now is");
     string str2 = " the winter";
	 cout << str1 << str2 << " of our discontent" << endl;
	 string str3 = str1 + str2 + "of our discontent";
	 cout << str3;
	 system ("pause");
return 0;
}


Im not getting 'file not found' so it is accessing data.txt, however its filling the screen with empty lines.
Last edited on
This doesn't look like a good idea:
1
2
3
4
5
	ofstream outData;
	outData.open("data.txt");

	ifstream inData("data.txt");
	inData.open("data.txt");


Do these really need to be the same file?
no, not particularly, ill try that. How do i specify what i want to get from data.txt? I've added a new output file; data2.txt, this doesn't change anything though, i still get a screen full of empty lines

EDIT: my new code, still outputs empty lines

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

int main() {

	
	string line;
	ifstream inData("data.txt");
	

    if(inData.is_open())
    {
        while(!inData.eof())
		{
            getline (inData,line);
			cout << line << endl;
		}
		inData.close();
    }else cout << "Cannot find this file " << endl;



   system ("pause"); 
return 0;
}
Last edited on
Topic archived. No new replies allowed.