I'm currently taking a introduction course to C++ and I've been able to do the first 3 assignments without much problems, but this new assignment is kicking my butt. The goal of this program was to make a simple text editing program. I simply open a saved text document and search for what I want to change and then replace it with what I want. The text I want to edit will only change the first or last parts of what I want or the entire thing. After I finish changing everything, I want to be able quit the editing process and save it as a new file or display it on the screen.

This is what I kind of want it to do
Filename:
Check if it opens successfully
Transfer the text into a variable
Edit the text in the variable, then when I'm done save it to a new file.
Somewhere along the line I have to close the file and check if it closes successfully also.

This is the code I have at the moment, but I'm having a lot of troubles with it. I need some help on what I should do and how to change this program :(

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
void filestatus (string, string, int, fstream);
string ChangeAll(string, string, string);
string ChangeRight(string, string, string);
string ChangeFirst(string, string, string);
void filestatus (string fina, string erme, int exco, fstream fs)
{
    if(fs.fail())
    {
        cerr << erme << fina << endl;
        exit(exco);
    }
}
string ChangeAll(string New, string Old, string s)
{
    string Left, Right;
    int P;
    while((P=s.find(Old))!= string::npos)
    {
        Left = s.substr(0, P);
        Right = s.substr(P + Old.size(), s.size());
        s = Left + New + Right;
    }
    return(s);
}
string ChangeRight(string New, string Old, string s)
{
    string Left, Right, snew = "";
    int P;
    while((P=s.rfind(Old))!= string::npos)
    {
        Left = s.substr(0, P);
        Right = s.substr(P + Old.size(), s.size());
        snew = Left + New + Right;
    }
    return(snew);
}
string ChangeFirst(string New, string Old, string s)
{
    string Left, Right, snew = "";
    int P;
    while((P=s.find(Old))!= string::npos);
    {
        Left = s.substr(0, P);
        Right = s.substr(P + Old.size(), s.size());
        snew = Left + New + Right;
    }
    return(snew);
}
int main()
{
    fstream fs, ofi;
    string fname, line, cont, newfname, inew, iold, choice;
    bool screen = false;
    bool all = true;
    cout << "File name please: ";
    getline(cin, fname);
    fs.open(fname.c_str(),ios::in);
    filestatus (fname, " is unable to open", 1, fs);
    while(getline(fs, line))
    {
        cout << line << endl;
    }
    fs.close();
    filestatus (fname, " is unable to close", 2, fs);
    for(;;)
	{
    cout << "Would you like to edit file?('yes' or 'no') ";
    cin >> cont;
    cin.ignore(256,'\n');
    if(cont == "no")
    {
    cout << "New output file name: ";
    getline(cin,newfname);
    if (newfname.empty())screen = true;
    if(screen) cout << line << endl;
    else       ofi << line;
    }
    else if (cont == "yes")
    {
    cout << "What would you like to change? ";
    cin >> iold;
    cout << "What would you like to replace it with? ";
    cin >> inew;
    cout << "Do you wish the change the first or last ";
    cout << "in every line or all?('first' or 'last' or 'all') ";
    cin >> choice;
    if (choice == "first")
    {
        ChangeFirst(inew, iold, line);
    }
    else if (choice == "last")
    {
        ChangeRight(inew, iold, line);
    }
    else if (choice == "all")
    {
        ChangeAll(inew, iold, line);
		cout << ChangeAll(inew, iold, line);
    }
    return(0);
	}
	}
}

Big thing is you're not storing all the input file

while(getline(fs, line))
    {
        cout << line << endl;
    }

reads a line from file, storing to string "line". Then it reads next line from file, overwriting whatever was in "line". So you'll only have the last line of data from the file when this is done.

Simple text editors, like we used to use in the bad ole days, were very much line oriented - you specified what line or range of lines you wanted to act upon. This can be implemented by reading in your file into an array of strings (or a vector if you choose, same concept)

So, you might set a maximum number of lines to handle, then allocate an array and read into it.

const int MAX_LINES = 20;

string lines[MAX_LINES];
int i = 0;

    while( i < MAX_LINES && getline(fs, lines[i]) )
    {
        cout << line << endl;
         i++;
    }

Your program will then have to be able to display any or all of the string elements of the array, and you'll need to get user input as to which line of data to process.

That's one way to read and store all the data, I'll leave other options as an exercise for the reader (or other forum members.)

You'll better read it to a vector of strings

string line;
vector<string> vs;
while (getline(fs, line))
{
  vs.push_back(line);
}
int numberOfLines = vs.size();

Then problem is I'm limited to what I already know and I'm not allowed to use stuff that the teacher havent taught yet. I know it's lame, but I'm stuck with it

Ok the teacher decided to teach us how to use arrays, but the teacher never told us how to edit the array. The way i have to edit each line in the array, is to dissect the sentence into 3 parts where 2 parts hold the information I want to keep and then change the 3rd piece into what I want. The question is how do I change information in array. I know I'm gonna need to use a increment value to move the next line in the array, but I'm not sure how to edit the array. The way I have to edit the array is to either change the first, last, or every occurrence. The first two I might be able to do it without many problems, the hard part is where i change it every time I see it.

void ChangeAllOccurence(string New, string Old)
{
    string Left, Right;
    int P;
	for(int i=0; i < 128; i++)
	{
		while((P=Buffer[i].find(Old))!= string::npos)
		{
		    Left = Buffer[i].substr(0, P);
		    Right = Buffer[i].substr(P + Old.size(), Buffer[i].size());
			newBuffer[i] = Left + New + Right;
		}
	}
}

void alter(string strN, string strO, string occur="all")
{
	if(occur == "all") ChangeAllOccurence(strN, strO);
	else if(occur == "first") ChangeLeftOccurence(strN, strO);
	else if (occur == "last") ChangeRightOccurence(strN, strO);
}

Ok so far the idea with the array is helping me a lot, but now I'm near the end, but I'm suffering what seems to be my last problem I hope. This purpose of these 2 codes is to take in what I want to change and replace it with what I want. Then I want to choose the method I want to change which is change it every time is see's it, but when I give it a test run it just freezes upon me. After all the input the program just runs static on me

This article has been dead for over six months. Start a new discussion instead.