I want to make a code that searches a text file and finds a certain string, and then changes that string to another string. First im going to have it input the whole file, and i am couting it too, so that i can make sure it worked. But it doesn't cout anything and the text file changes to a bunch of random characters. Anyone know how to fix this? This is my code so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
	char File[150], re[4], line[100][125];
	string response;
	int counter=0;
    cout<<"Would you like to convert a SRL 3.81 script to SRL 4? (yes or no) \n";
	cin>>re;
	strupr(re);
	response=re;
	if(response== "YES") {
		cout<<"Please enter the name of the file (C:/Program Files/example.txt)"<<endl;
		cout<<"the location is not needed if this program and the script are in the same folder\n";
	    cin.ignore ( 150, '\n' ); //flush the input stream
		cin.getline(File, 150);
	    cout<<"Now Editing "<< File<<endl;
		ifstream fin;
		ofstream fout;
		fout.open(File);
		fin.open(File);
		while (!fin.eof()){
			fin.getline(line[counter], 125);
			counter++;
		}
		for(counter=0; counter<10; counter++){		
			cout<<line[counter]<<endl;
		}
		fin.close();
		fout.close();
	}
	else if(response == "NO"){
		cout<<"Closing Program...\n";
	  }
	else{
		cout<<"Response not understood. Closing Program...\n";
	}
	system("PAUSE");
    return 0;
}

OK. First thing first. You're using C++ so you should think about using std::string rather than statically sized character arrays. Benefits - dynamically resized, memory automatically managed, public methods, easy to use operators (like equality). Downsides - you're going to have to learn them.
Especially since you "#include <string>".

Also if you're going by words you might as well do something like:

// Prep work like setting up input stream, testing it etc.
std::string str;
while( in >> str ) {
  std::cout<< "This is a word: " << str << "\n";
  // etc...
}

Note or warning - that stops at whitespace. Not at punctuation so you're going to have to think about that.

I don't think you're going to be able to open a stream to both write and read a file at the same time. Open a stream. Read the contents. Close the stream. Open another one. Output the contents. Close that stream then.

Something else to consider is using a vector to store all the stuff you read rather than a 2D array.

not really sure what vectors are...but im going to use new and delete to fix my arrays once my dad teaches me. Also, with the char 2d array, should i have it like:

char line [how many chars in a line][how many lines]

or

char line [how many lines][how many chars in a line]

if either will work, how should i do my while loop

It's the latter, I think. Look at vectors later on. In short they're expandable arrays which aren't set in size on build.

He wants to store the whole file, I believe.

You know where you fin.open and fout.open. Only deal with one aspect of the proess at a time. First open the file and store the contents. Then close it and open the out file and write the contents you have stored by reading from the first file. Then close the output file.

So change:

ifstream fin;
        ofstream fout;
        fout.open(File);
        fin.open(File);
        while (!fin.eof()){
            fin.getline(line[counter], 125);
            counter++;
        }
        for(counter=0; counter<10; counter++){        
            cout<<line[counter]<<endl;
        }
        fin.close();

to

ifstream fin;
                // READ THE INPUT FILE
                fin.close()


        fout.open(File);
                // WRITE THE OUTPUT FILE
        fout.close();

>> is there a better way to do it?

Run this:

#include <string>
#include <iostream>
#include <fstream>

using namespace std;

int main( )  {
  string line, word;
  ifstream in;


// ONE WAY OF READINF FROM A FILE
  in.open( "FILENAME.TXT" );

  if ( in ) // If stream opened successfully
    while ( std::getline( in, line ) )
      cout<< line << "\n";
  in.close();


// OTHER WAY OF READING FROM FILE
  in.open( "FILENAME.TXT" );

  if ( in )
    while ( in >> word )
      cout<< word << " ";
  in.close();

  return 0;

}

And see if you can understand it. THe first way reads a file line by line and the other way does it word by word.

Please answer my question:
Is the string you need to change confined to one line?
If it is not neither example you have been shown will work.

no it is not

but if that really complicates things, then it is ok

If the string you need to find is not confined to one line, then reading through the text file line by line won't work.

A simple solution, would be to concatenate each line together and treat the entire text file as one long string.

Searching it would be the same as you search any other string. Perhaps you might need to take a little extra care with the left over '\n' (newlines) either discarding them altogether or keeping them.
I'm not sure.

This question has already been answered. Start a new discussion instead.