Replacing text in file

Hey,

I'm attempting to write a simplish program for an assignment. The goal of the program is to change text in a file. We are learning string functions and file io.
I believe the syntax of the program is correct, but it still won't compile. I don't understand the compiler error and google didn't help.

Thanks,
KC


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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

									//QUICK DESCRIPTION
int checkFiles(ifstream, ofstream);					//Checks files are good
void displayString(ifstream);						//Displays orig text
string queryString();							//Displays query
ofstream findReplaceTxt(string, ifstream, ofstream);			//Replaces text
void outputFinish(ofstream, ifstream);					//Closes and finishes


int main()
    {
    ifstream inputFile ("input.txt");					//Opening files
    ofstream outputFile ("output.txt");

    string inputString;							//This string keeps the 
									//returned user replaced
	int i;								//word.
	checkFiles(inputFile, outputFile);				//Function checks the open
		if (i = 1) return 1;					//files, if I == 1 its bad

    displayString(inputFile);						//Sending input file to display
    inputString = queryString();					//Recieving user input
    outputFile = findReplaceTxt(inputString, inputFile, outputFile);	//Replacing text
    outputFinish(outputFile, inputFile);				//Sending files to close
    return 0;							        //Exit
    };


int checkFiles(ifstream inputFile, ofstream outputFile)			//This function checks the files
	{								//to see if they were opened 
	if (inputFile.fail() || outputFile.fail())			//succcessfully, if they were
		{							//not opened it returns a 1
		cout << "Bad input";
		return 1;
		};
	};
    

void displayString(ifstream inputFile)					//This function displays the
    {									//origional text file
    cout << inputFile << endl;
	cout << "--------------------" << endl;
    return;
    };
    

string queryString()							//This function asks the user
    {									//what he wants to replace
	string inputString;						//then sends that back as a 
    cout << "What text do you want to replace?";			//string
    getline(cin, inputString);
    return inputString;
    };


ofstream findReplaceTxt(string inputString, ifstream inputFile, ofstream outputFile)
	{								//This function takes the files
	string outputString;						//and 1) put the text in a string
	while(!inputFile.eof());					//2) replaces the text and 3) puts
        {								//the string back into output file
	    getline(inputFile, outputString);
		outputString.replace( outputString.find("inputString") , inputString.size() , inputString );
		outputFile << outputString;
        };

    return outputFile;
    };
    

void ouputFinish(ofstream outputFile, ifstream inputFile)		//This function closes the 
    {									//files then pauses the system
	inputFile.close();						//so the user can see the final
    outputFile.close();							//output before the program
    cout << endl << "--------------------" << endl;			//exits
	cout << "The new file was saved at Output.txt" << endl;
    system("PAUSE");
    };




Here is the error, i get a similar error with bloodshed dev. I tried adding & to some of the input vars because of the could not access errors. Still no luck.
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
1
1>  Project 2.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(1116): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_ofstream<_Elem,_Traits>::basic_ofstream(const std::basic_ofstream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(890): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\ios(176) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
1>          This diagnostic occurred in the compiler generated function 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const std::basic_ifstream<_Elem,_Traits> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>
1>          ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
tl;dr, but if you're wanting to edit part of a text file without rewriting the whole thing, it's not possible. You must rewrite the whole file and add or omit the change.
1
2
ofstream findReplaceTxt(string, ifstream, ofstream);
outputFile = findReplaceTxt(inputString, inputFile, outputFile);
The copy constructor of ofstream is private, pass it by reference instead (return a reference or void)

Edit: Actually ios_base(const ios_base&) is private, so the same applies for ifstream.

Also
1
2
3
	int i;
	checkFiles(inputFile, outputFile); //statement has no effect
		if (i = 1) return 1; //I think you want i==1 (but you never assigned anything to i) 
Last edited on
Okay got it thanks!
Topic archived. No new replies allowed.