ok so i made a code that makes something compatible with a never version ( it is not important). This is my code:

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

int main () 
{
	char File[150], re[4]; 
	string line[5000];
	string response;
	int counter=0;
	size_t found;
	string strtofind[]={"oolean1", "oolean2", "oolean3", "nteger1", "nteger2", "nteger3",
		"tring1", "tring2", "tring3"};
	string changestr[]={"ooleans[0]", "ooleans[1]", "ooleans[2]", "ntegers[0]", "ntegers[1]", 
		"ntegers[2]", "trings[0]", "trings[1]", "trings[2]"};
	int index;


	cout<<"         Script Converter\n";
    cout<<"Would you like to convert a SRL 3.81 script to SRL 4? (yes or no) \n";
	cin>>re;
	_strupr_s(re);
	response=re;
	if(response== "YES") {
		cout<<"Please enter the name of the file (C:/Program Files/example.scar)"<<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 Converting "<< File<< " to SRL 4 ..."<<endl;
		ifstream fin;
		ofstream fout;
		fin.open(File);
		if(fin){
			while (getline(fin, line[counter])){
				counter++;
			}
			for(counter=0; counter<5000; counter++){		
				for(index=0; index<11; index++){
					found=line[counter].find(strtofind[index]);
					if (found!=string::npos){
						line[counter].replace(int(found), strtofind[index].length(), changestr[index]);
					}
				}
			}
			fout.open(File);
			fout<<"{Script Converted to SRL 4 by Macrosoft's Script Converter}\n";
			for(counter=0; counter<5000; counter++){
				fout<<line[counter]<<"\n";
			}
			cout<<"Finished converting script\n";
		}
		else {
			cout<<"Failed to open file\n";
		}
		fin.close();
		fout.close();
	}
	else if(response == "NO"){
		cout<<"Closing Program...\n";
	  }
	else{
		cout<<"Response not understood. Closing Program...\n";
	}
	system("PAUSE");
    return 0;
}

it works fine when i debug (i use ms visual C++). But when i use the build version, it always "encounters a problem" and has to close. If someone knows why this is happening, that would be great.

Thanks in advance

Dani AI

Generated

A release-only crash like "has encountered a problem" almost always means undefined behavior that the debug build happened to hide. As and hinted, the most likely causes in the posted code are out‑of‑bounds access and unsafe assumptions (fixed-size arrays, magic loop limits, and opening the output over the input). Those create memory corruption that can be benign in debug but fatal in optimized/release code.

Concrete fixes and rules to apply:

  • Never iterate to hardcoded limits. Use the actual container size (e.g. container.size()) or compute the array length; avoid “5000” or “11” magic numbers.
  • Replace raw fixed arrays for lines or mappings with std::vector or a container of pairs so bounds are automatic.
  • Do not open the output file using the same filename while the input stream is still open; write to a temp file and rename on success.
  • Keep types correct: string::find returns size_t; avoid casting to int when calling replace. Use size_t for positions.
  • Turn on high warning levels and run the release EXE under the debugger (enable debug information for the Release build) or add simple logging to see the last successfully processed line.

Safe processing pattern (example — adapt the replacement list and error checks to taste):

#include <fstream>
#include <string>
#include <vector>
#include <utility>
#include <cstdio> // remove/rename

bool convert(const std::string& path) {
    std::ifstream fin(path);
    if (!fin) return false;
    std::string tmp = path + ".tmp";
    std::ofstream fout(tmp);
    if (!fout) return false;

    std::vector<std::pair<std::string,std::string>> map = {
        {"oolean1","ooleans[0]"}, {"oolean2","ooleans[1]"} /* ... */
    };

    std::string line;
    while (std::getline(fin, line)) {
        for (auto &p : map) {
            for (size_t pos = 0; (pos = line.find(p.first, pos)) != std::string::npos; )
                pos += (line.replace(pos, p.first.size(), p.second), p.second.size());
        }
        fout << line << '\n';
    }
    fin.close(); fout.close();
    std::remove(path.c_str());
    std::rename(tmp.c_str(), path.c_str());
    return true;
}

Those changes remove the usual UB triggers and make the program robust in both debug and release.

Recommended Answers

All 13 Replies

what "problem" does it say it's having? Copy the error log and post it here so that we can see it.

no, you dont understand

it compiles.

but when i run the build version, it either can open the file, or a window pops up saying


SRL 3.81 to SRL 4.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

>SRL 3.81 to SRL 4.exe has encountered a problem and needs to close.
Classic runtime error. It means you're probably overrunning a buffer somewhere. Run your program in debug mode and step through it until you hit the error. That'll give you a good idea of where the error is being manifested, and you can trace it back the cause from there.

i dont get error when i run it in debug mode...

>i dont get error when i run it in debug mode...
Then sprinkle debug messages everywhere and run it in release mode. Are you really this helpless or are you just trying to get us to do your troubleshooting for you?

> If someone knows why this is happening, that would be great.
I've got two pretty good ideas where the problem is (ok, 3 actually).

> while (getline(fin, line[counter]))
What stops this over stepping the array?

> for(counter=0; counter<5000; counter++)
Why 5000 and not the number of lines you read?.
Since this is the same variable, why did you even bother with counting them?

> for(index=0; index<11; index++)
How many entries are there in strtofind ?

Come to think of it, why do you even bother storing the whole file in memory at all. "Read a line, do the subs, write out the line" would work just as well in a loop.

>i dont get error when i run it in debug mode...
Then sprinkle debug messages everywhere and run it in release mode. Are you really this helpless or are you just trying to get us to do your troubleshooting for you?

i didn't get what you just said, yes i am that helpless

> If someone knows why this is happening, that would be great.
I've got two pretty good ideas where the problem is (ok, 3 actually).

> while (getline(fin, line[counter]))
What stops this over stepping the array?

> for(counter=0; counter<5000; counter++)
Why 5000 and not the number of lines you read?.
Since this is the same variable, why did you even bother with counting them?

> for(index=0; index<11; index++)
How many entries are there in strtofind ?

Come to think of it, why do you even bother storing the whole file in memory at all. "Read a line, do the subs, write out the line" would work just as well in a loop.

>What stops this over stepping the array?
huh?
>Why 5000 and not the number of lines you read?.
>Since this is the same variable, why did you even bother with counting them?
already fixed it, but im trying to use new and delete for the array of line, but running through some problems :S

>How many entries are there in strtofind ?
9, dk why i put 11...

>Come to think of it, why do you even bother storing the whole file in memory at all. "Read a line, do
> the subs, write out the line" would work just as well in a loop.
well, i think that if you open a ofstream, everything else in the file is deleted, am i correct?

ill post the problem with new and delete on another thread in a little bit, my moms guna kill me :(
(i need to do hw)

> >What stops this over stepping the array?
> huh?
What happens if the file has more than 5000 lines?

> but im trying to use new and delete for the array of line
Consider using a std::vector then, and save yourself a lot of confusion.
Besides, you should concentrate on getting this one to work rather than trying to rewrite it whilst it still has bugs.

> well, i think that if you open a ofstream, everything else in the file is deleted, am i correct?
Personally, I would create a temp file, then only replace the old file when I know the new file is created successfully.
If for some reason your program crashed soon after the fout.open(File); you'd lose the lot.

how do i use vectors?

temp file? i would run into the same problem, wouldn't i?

> how do i use vectors?

std::vector<std::string> lines;
std::string aLine;
while ( getline(fin, aLine) ) lines.push_back(aLine);

You can then use lines.size() and a for loop to iterate over the vector, exactly like you would do using an array.


> temp file? i would run into the same problem, wouldn't i?
No.
You read from the input file, and write to the temp file.
You then close both files.
You then delete the original input file.
You then rename the temp file back to the original file.
The only danger is that you MAY end up with all your data in the temp file, but you will never outright lose all your data.

little more info on vectors please

i dont have a book...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.