Program keeps crashing


Im writing a code that requires the input and output of a file which is typed on the command line. the output file would append or overwritten if it exists. im having difficulty running this as everytime i ran it the program crashes. and i hve already tried debugging it and it would say segfault.. any tips would be appreciated


#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

void creating_array(string* &list, int argc , char **argv);
void find_1(string *list, int argc, string in_file_name, string what);
void find_2(string *list, int argc, string out_file_name, string what);
void checking_file(ifstream &inputfile, ofstream &outputfile, string in_file_name , string out_file_name);
void reading_file(ifstream &inputfile);
void append_overwrite(string out_file_name , string in_file_name ,ifstream &inputfile, ofstream &outputfile);
void writing_data(ofstream &outputfile, ifstream &inputfile, string in_file_name, string out_file_name);
int main(int argc, char **argv)
{
string in_file_name, out_file_name;
ifstream inputfile;
ofstream outputfile;
string *list;



creating_array(list, argc, argv);
find_1(list,argc,in_file_name, "/i");
find_2(list,argc, out_file_name, "/o");
checking_file(inputfile, outputfile, in_file_name , out_file_name);
reading_file(inputfile);
append_overwrite(in_file_name , out_file_name,inputfile, outputfile);
writing_data(outputfile, inputfile, in_file_name,out_file_name);
delete [] list;

return 0;
}
void creating_array(string* &list, int argc, char **argv)
{
list = new string[argc];
for (int i = 1; i <= argc ; ++i)
list[i] = argv[i];
}
void find_1(string *list, int argc, string in_file_name,string what )
{
vector<int> who;
for(int i = 0; i < argc; ++i)
if(list[i].find(what) < string::npos)
who.push_back(i);
if(who.size() == 0)
cout<<"The word " << what << " does not appear on the command line"<<endl;
else
{
cout<< what <<" is in these arguments from the command line:"<<endl;
for(int i = 0; i < who.size();++i)
{
cout<<"argv["<<i<<"] = " <<list[who[i]]<<endl;
list[who[i]] = in_file_name;
}
}
}
void find_2(string *list, int argc , string out_file_name, string what)
{
vector<int> who;
for(int i = 0; i < argc; ++i)
if(list[i].find(what) < string::npos)
who.push_back(i);
if(who.size() == 0)
cout<<"The word " << what << "does not appear on the command line"<<endl;
else
{
cout<< what <<" is in these arguments from the command line:"<<endl;
for(int i = 0; i < who.size();++i)
{
cout<<"argv["<<i<<"] = " <<list[who[i]]<<endl;
list[who[i]] = out_file_name;
}
}
}

void checking_file(ifstream &inputfile, ofstream &outputfile, string in_file_name , string out_file_name)
{
inputfile.open(in_file_name.c_str());
if(inputfile.fail( ))
{
cout << "Input file opening failed.\n";
exit(0);
}
outputfile.open(out_file_name.c_str());
if(outputfile.fail( ))
{
cout << "Output file opening failed.\n";
exit(0);
}

}
void reading_file(ifstream &inputfile)

{
string line;
if(inputfile.is_open())
{
while(getline(inputfile,line))
{
cout << line << '\n';

}

}
else cout << "Unable to open file";


}

void append_overwrite(string in_file_name , string out_file_name ,ifstream &inputfile, ofstream &outputfile)
{
char seven[1];
string answerz;
bool again = true;
while(again)
{

inputfile.open(in_file_name.c_str());
cout << "Does the output file exist? (Press y for yes or n for no) " << endl;
cin >> seven[1];
if((seven[1] == 'y' )|| (seven[1] == 'Y'))
{
cout << "Enter /a to append, /e to overwrite, /o to quit and /r to reenter" << endl;
cin >> answerz;

switch (answerz[0])
{
case 'a' :

outputfile.open(out_file_name.c_str(),ios::app);

again = false;
break;

case 'e':


outputfile.open(out_file_name.c_str());

again = false;
break;

case 'o':
exit(0);

default : cout << "Unknown option, please try again" << endl;
}
}
else if ((seven[1] == 'n') || (seven[1] == 'N'))
{
exit(0);
}
else
{
cout << "Wrong answer please type in y or n.." << endl;
}
}
}
void writing_data(ofstream &outputfile, ifstream &inputfile, string in_file_name, string out_file_name)
{

if(outputfile.is_open())
{
outputfile << in_file_name.c_str();
outputfile.close();
inputfile.close();
}
else cout << "Unable to open file";


}

Last edited on
The first thing you should look for when it crashes is an out of bounds access.

Notice that an array always starts with 0 and the last valid index is always the number of array elements - 1

Here:


for (int i = 1; i <= argc ; ++i) // Note: It must be < argc no matter how you start

cin >> seven[1]; // Note: 1 is invalid. 0 is the only valid index for this array.



Another thing:

find(...) returns string::npos if not found hence everything not string::npos is ok.

if(list[i].find(what) < string::npos) // Note: != string::npos
Topic archived. No new replies allowed.