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?

Well, as nice as command != ('A' || 'B') looks it doesn't work like that. if( command != 'A' || command != 'B' ) is what you're looking for, but to be honest a straight out else should do -- it's proven to neither be an 'A' or 'B'.

Not sure if that's your problem now. Don't have linux.

Thanks. I hadn't noticed that, and its fixed now, but that didn't really effect the program any.

But, like I said, it does work under Windows, just not Linux.

My program works properly under windows, however, it seg faults when I try to compile/run it under linux.

So, how did you get the executable program when there were compile erorrs? No respectable compiler produces the executable under that condition. Do you not look at the messages your compiler produces?

But, like I said, it does work under Windows, just not Linux.

The exact same code ? Is yes, then its impossible you could have gotten a clean compile.

I'm sorry I wasn't clear. I meant that I had compiled it under linux (successfully) to get a .out file. Executing the .out file does not work though. Compiling under windows to gets me a working, non seg-faulting .exe file.

I hope that made sense.

No it doesn't make sence. What compiler are you using ? No compiler in the world would have given you a clean compile with those errors in it. You just did not pay any attention to the errors your compiler gave you.

> My program works properly under windows, however, it seg faults when
> I try to compile/run it under linux.
Unfortunately, that just makes you lucky, not good.

Code sometimes works, despite your best attempts to muck it up. Yet at other times, even the most minor transgression is severely punished.

> strcpy(location, (location + strlen(fileline)));
Overlapping copies using strcpy() are undefined (meaning anything can happen - like working vs. seg fault).
Try copying via a second array.

Why are you using messy C char arrays in a C++ program by the way?

Yes, I realize using char arrays is terrible, but those were assignment requirements.

Thanks to everyone who replied, but I've got it working now.

Yes, I realize using char arrays is terrible, but those were assignment requirements.

Thanks to everyone who replied, but I've got it working now.

What did you do to resolve the problem?

I recopied the input file I was reading from, saved it under a different name, and it worked fine. Nothing was changed in either the code or the file, so I don't really know why it was giving me so much problems.

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.