| | |
c++ homework seg faults
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 7: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 6:23 pm.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
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.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
> 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?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
--
If your code lacks code tags, you will be IGNORED
--
If your code lacks code tags, you will be IGNORED
![]() |
Other Threads in the C++ Forum
- Previous Thread: Array trouble
- Next Thread: Hello I need help on a random number generator. PLEASE
Views: 1038 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C++
algorithm api array arrays assignment basic beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete desktop dll dynamic encryption error file files form fstream function functions game givemetehcodez graph gui homework i/o iamthwee input int integer lazy library linker list loop looping math matrix member memory network newbie news number numbers object objects opengl output parameter pointer pointers problem program programming project qt random read recursion recursive reference return search sort sorting spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock






