file stream, read from one text file and put the text into new file

Mar 6, 2014 at 11:16am
I have an excersise, where i have to read text from a text file in read mode and the copy that text to another text file in write mode, so far its all ok with reading the file, but stuck on placing that output in another.

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

int main()
{
    fstream mans;
    fstream mans2;
    char c;
   
    mans.open("my_file.txt", ios::in);
   mans2.open("my_file2.txt", ios::out);
  
  mans.get(c); //reading first file
    while (mans)
    {
     cout<<c;
         mans.get(c);
    };
   
  //need to place the text from my_file.txt to my_file2.txt besides in uppercase.
mans.close();
mans2.close();

//check, if the text is placed into other file
char a;        
mans2.open("my_file2.txt", ios::in);
mans2.get(a);
 while (mans2)
    {
          cout<<a;
          mans.get(a);
          };
mans2.close();

system("pause");
return 0;
    
}
Last edited on Mar 6, 2014 at 11:30am
Mar 6, 2014 at 11:41am
Line 18: you do write to a stream. Which stream should you write to here?
Mar 6, 2014 at 11:42am
Use the put() function:

http://www.cplusplus.com/reference/ostream/ostream/put/?kw=put


or so:
1
2
3
4
5
    while (mans >> c) //reading first file
    {
     cout<<c;
     mans2 << c;
    }
Mar 6, 2014 at 12:10pm
yeah..i thought so too but none of this works :( i am missing something.
Mar 6, 2014 at 1:15pm
Please explain the "none works".
Mar 6, 2014 at 2:43pm
1
2
3
4
5
6
7
8
9
10
11
mans2<<c:
mans.get(c);

//tried it like this
while (!mans.eof())
{
cout<<c;
mans.get(c);
mans2<<c;
mans2.put(c);
}
Last edited on Mar 6, 2014 at 2:44pm
Mar 6, 2014 at 3:21pm
both text files should contain the same text in the end
Mar 7, 2014 at 7:01am
Either put(c) or << not both.

If you write it like so the end of file is not properly handled (if get(c) detects an error like eof c is written to the output file regardless)
Topic archived. No new replies allowed.