Function trouble in a string substitution program

Basically I'm supposed to write a program that asks for two file names, an input and an output file. The program then reads the input file and makes substitutions for the file to say something totally different in the output. I'm having trouble writing some of the functions, specifically where I'm trying to get it to open the file location at infile.open(location);. Could anyone point me in the right direction?

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;

bool fileTRUE(string filename);
void all(string &data);
void first(string &data);
void penultimate(string &data);


int main()
{

    
    string location, text, location2;
    ofstream outfile;
    ifstream infile;
    
    
    cout<<"Enter the name of your text file, followed by .txt, for example: \"textfile.txt\"\n";
    getline(cin, location);
    
    
    
    if(fileTRUE(location)==false)
        {
            cout<<"That file does not exist";
            return 0;
        }
    
    
    
    cout<<"Enter the name of the text file to which you would like to save the modified data\n";
    getline(cin, location2);
    
    
    
    infile.open(location)
    
    
    while(getline(infile, location))
        text+=location;
    
    
    all(text);
    first(text);
    penultimate(text);
    
    
    
    outfile.open(location2);
    outfile<<text;
    outfile.close();
    
    
    cout<<"Modification complete. Please check files";
    return 0;

}


bool fileTRUE(string filename)
{
    
    ifstream check;
    check.open(filename);
    if(check)
        return true;
            else return false;

}


void all(string &data)
{
    
    
    
    
    size_t index;
    while ((index = data.find("all")) != string::npos )
        data.replace(index, 3, "some");
    
}


void first(string &data)
{
    
    
    size_t index;
    while ((index = data.find("first")) != string::npos )
        data.replace(index, 5, "second");
    
}


void penultimate(string &data)
{
    
   
    
    size_t index;
    while ((index = data.find("the last")) != string::npos )
        data.replace(index, 8, "the penultimate");
    
}
You need a semicolon at the end of line 39.
I accidentally left that out when I was playing with the code trying to find a solution.

I think I fixed the infile and outfile opening portion by using the following:

infile.open(location.c_str());

but now I'm having trouble with this particular function:

1
2
3
4
5
6
7
8
9
10
bool fileTRUE(string filename)
{
    
    ifstream check;
    check.open(filename);
    if(check)
        return true;
            else return false;

}
What do you mean by having trouble? Also, you could just have return check, but that wouldn't change the end logic.

Edit: using cstrings shouldn't be necessarily.
Also, I just tried out the program and it seems to work fine, it copied the contents of the first file to the second.

You might be doing something with the files that your OS doesn't like, make sure they're closed first.
Last edited on
Topic archived. No new replies allowed.