What am i doing wrong?

Ok im making a program that allows you to open a text file in the cmd window, then it should allow you to find a word from what was inputted and change it to something different, but when i try it, it says something alongs the lines that the proigram was asked to execute in an unusual way and has crashed or something. What am i doing wrong?

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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void find(string& storeText, ifstream& fileIn);
int updateHistory(int& r);

int main()
{
    string fileName;
    string storeText;
    int r = 0;

    cout << "What file do you want to open?" << endl;
    cout << "The file you want to open must be typed in EXACTLY as it" << endl;
    cout << "appears on your computer; for example: document.txt\n" << endl;
    getline(cin, fileName);

    if(fileName == "updatehistory")
    {
        updateHistory(r);
    }
    if(r == 10)
    {
        cout << "File Name" << endl;
        getline(cin, fileName);
    }

    ifstream fileIn;
    fileIn.open(fileName.c_str());

    cout << "\n";

    if(fileIn.fail())
    {
        cout << "Failed to open" << endl;
        fileIn.close();
    }

    while(getline(fileIn, storeText))
    {
        cout << storeText << endl;
    }
    cin.get();

    find(storeText, fileIn);

    fileIn.close();
}

void find(string& storeText, ifstream& fileIn)
{
	string str2;
	cout << "Which word you want to find from the document?: ";
	cin >> str2;
	cin.get();
	size_t found;

	found = storeText.find(str2);
	cout << "Enter a word: ";
	string str3;
	cin.ignore();
	getline(cin,str3);

	cout << "After replacing with the given word: \n" << endl;
	storeText.replace(storeText.find(str2), str2.length(), str3);

    while(!fileIn.eof())
    {
        cout << storeText << endl;
    }

	cin.get();
}

int updateHistory(int& r)
{
    cout << "VERSION INFORMATION\n" << endl;

    cout << "Version: 1.0.0 Alpha" << endl;
    cout << "Recently added features" << endl;
    cout << "1. Added replace function\n" << endl;

    cin.get();

    return r = 10;
}
The crash is on line 68. storeText.find(str2) returns std::string::npos which means that the sting was not found. Passing std::string::npos to replace will throw an exception because it's not a valid position. You need to check the return value of find before passing it to replace.

When you call getline(fileIn, storeText) storeText will get a new value. If the call to getline fails it will leave storeText empty. That means you have not acutally stored the file content anywhere. After the loop on line 42-44 storeText will always be empty so you are always passing an empty string to ::find.
oh ok i'll work on trying to fix that but how can i make an error check for that problem? like i did here:

1
2
3
4
5
  if(fileIn.fail())
    {
        cout << "Failed to open" << endl;
        fileIn.close();
    }
Ok i tried some things and im totally confused.
1
2
3
4
5
found = storeText.find(str2);
if (found == std::string::npos)
{
	// Do whatever you want to do in case str2 was not found in storeText.
}
Ok cool, now about the other problem, im still confused.
If you want storeText to contain all lines in the file you can use another string to read each line with getline and append it to the end of storeText.

1
2
3
4
5
std::string line;
while(getline(fileIn, line))
{
	storeText += line + '\n';
}


and i put this in my find() function? replacing:

1
2
3
4
 while(!fileIn.eof())
    {
        cout << storeText << endl;
    }
No was thinking about the loop main.

I don't know what you want to do with fileIn inside find. If you want to write to file you need an ofstream, instead of an ifstream.
Last edited on
What i want my program to do is open the file in main, then the function find, will find and replace a word in the loaded document. Or do i need to load the document again in the find function?
You need to open the file again but as an ofstream. Make sure to close fileIn before doing that. fileIn.close();
Ok i have been trying this for a while now iand i cant get it working, can you show me what the final code looks like?
bump
Topic archived. No new replies allowed.