954,148 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

c++ homework seg faults

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?

tamereth
Newbie Poster
5 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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.

twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
 

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.

tamereth
Newbie Poster
5 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 
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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

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.

tamereth
Newbie Poster
5 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,042 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,341
 

> 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?

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

tamereth
Newbie Poster
5 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

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?

superjacent
Junior Poster in Training
66 posts since Nov 2007
Reputation Points: 11
Solved Threads: 3
 

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.

tamereth
Newbie Poster
5 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You