My program works properly under windows, however, it seg faults when I try to compile/run it under linux.
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
char command;
char infilename[80];
char outfilename[80];
char fileline[80];
char nextline[80];
char temp[80];
char temp2[80];
char * location;
char sentence[250];
int loc = 0;
char log[250][80];
int x = 0;
int y = 0;
fstream infile;
fstream outfile;
cout << "Enter instructions file name: ";
cin >> infilename;
infile.open(infilename, ios::in);
outfile.open("output", ios::out);
if(!infile)
{
cout << "File doesn't exist, exiting.\n";
exit(1);
}
infile.getline(sentence, 80);
outfile << sentence << endl;
do
{
infile >> command;
infile.getline(fileline, 80);
cout << command << fileline << endl;
if(command == 'I')
{
infile >> command;
infile.ignore(10, ' ');
infile.getline(nextline, 80);
cout << command << " " << nextline << endl;
if(command=='A')
{
location = strstr(sentence, nextline);
location += strlen(nextline);
strcpy(temp2, location);
strcpy(location, fileline);
location += strlen(fileline);
strcpy(location, temp2);
location += strlen(temp2);
strcpy(temp, "");
x++;
outfile << sentence <<endl;
}
else if(command=='B')
{
location = strstr(sentence, nextline);
strcpy(fileline, fileline + 1);
strcat(fileline, " ");
strcpy(temp2, location);
strcpy(location, fileline);
location += strlen(fileline);
strcpy(location, temp2);
location += strlen(temp2);
outfile << sentence << endl;
strcpy(temp, "");
x++;
}
else if(command != ('A' || 'B'))
{
cout << "Input file corrupt, exiting...\n";
}
}
else if(command == 'R')
{
location = strstr(sentence, fileline);
strcpy(location, (location + strlen(fileline)));
outfile << sentence << endl;
x++;
}
else
{
cout << "Input file corrupt, exiting...\n";
}
}
while(!infile.eof());
infile.close();
outfile.close();
char readin[80];
outfile.open("output", ios::in);
while(!outfile.eof())
{
outfile.getline(readin, 80);
cout << readin << endl;
}
outfile.close();
}
It segfaults after reaching
cout << command << " " << nextline << endl;
It will display the line, but will not enter the if statements. It will not get to a cout statement right after it, nor will it work properly if I just have
cout << command << " ";
It seems like the cout statement is causing the segfault, but that can't be right, can it?