Files and strings. C++

closed account (N1hqko23)
Hello. I am begginer and I need your help, because I have tried almost everything possible in my situation and have no idea how to do right way.
My task was following:
in the first text file we have some text, we need to replace all words "sun" to "moon" and write down the result to other text file using fstream function.
My problem is in the cycle of replacing. I was said to look more attentively at my indexes. I did it. Many times, but without result. Here is my code, which works as I want, but with error in the end.

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

int main()
{
	fstream in("D:\\test_in.txt", ios::out | ios::trunc);
	if (!in)
	{
		cerr << "Cannot open the file!";
		return 0;
	}

	string inT;
	cout << "Enter your text with word sun \n" << endl;
	getline(cin, inT);
	in << inT.c_str();
	cout << "Text is written" << endl;
	
	fstream out("D:\\test_out.txt", ios::out | ios::trunc);
	if (!out)
	{
		cerr << "Cannot open the file!\n";
		return 0;
	}

	string outT;
	string ToFind = "sun";
	string ReplaceWith = "moon";
	
	for (string::iterator it = inT.begin(); it != inT.end(); ++it)
		{ outT = inT;
		outT = inT.replace(inT.find(ToFind), ToFind.size(),  ReplaceWith);
		out << outT.c_str();
		cout << "Text after replacing " << outT << endl;
		}
		
	in.close();
	out.close();

	system("pause");
	return 0;
}
Assuming that words are separated by a single space, and ignoring punctuation:

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
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <fstream>
#include <string>
// using namespace std;

int main()
{
    const char* const in_file_name = "D:\\test_in.txt" ;
    const char* const out_file_name = "D:\\test_out.txt" ;

    {
        std::ofstream fout(in_file_name); // open file for output
        if( !fout.is_open() )
        {
            std::cerr << "could not open file\n" ;
            return 1 ;
        }

        std::string inT;
        std::cout << "Enter your text with word sun \n\n" ;
        std::getline( std::cin, inT ) ;
        fout << inT << '\n' ;
        std::cout << "Text is written" << '\n' ;
        // fout is automagically closed  when we exit the block
    }

    {
        std::ifstream fin(in_file_name); // open the file for input
        std::ofstream fout(out_file_name); // open output file
        if( !fin.is_open() || !fout.is_open() )
        {
            std::cerr << "could not open file\n" ;
            return 1 ;
        }

        const std::string ToFind = "sun";
        const std::string ReplaceWith = "moon";

        std::string word ;
        while( fin >> word ) // for each word in the input file
        {
            if( word == ToFind ) fout << ReplaceWith << ' ' ; // replace
            else fout << word << ' ' ; // do not replace
        }
        fout << '\n' ; // end with a new line
    }

    // dump the contents of test_out.txt to stdout
    std::cout << "text after replacing: " << std::ifstream("D:\\test_out.txt").rdbuf() ;
}
Topic archived. No new replies allowed.