| | |
c++ homework seg faults
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jan 2008
Posts: 5
Reputation:
Solved Threads: 0
My program works properly under windows, however, it seg faults when I try to compile/run it under linux.
It segfaults after reaching
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
It seems like the cout statement is causing the segfault, but that can't be right, can it?
c++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
cout << command << " " << nextline << endl;
C++ Syntax (Toggle Plain Text)
cout << command << " ";
It seems like the cout statement is causing the segfault, but that can't be right, can it?
Last edited by Ancient Dragon; Jan 28th, 2008 at 8:19 pm. Reason: add line numbers
Well, as nice as
Not sure if that's your problem now. Don't have linux.
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.
•
•
•
•
My program works properly under windows, however, it seg faults when I try to compile/run it under linux.
The exact same code ? Is yes, then its impossible you could have gotten a clean compile.
Last edited by Ancient Dragon; Jan 28th, 2008 at 7:23 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
> 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?
> 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?
![]() |
Other Threads in the C++ Forum
- Previous Thread: Array trouble
- Next Thread: Hello I need help on a random number generator. PLEASE
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop developer directshow dll download dynamic encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






