c++ homework seg faults

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jan 2008
Posts: 5
Reputation: tamereth is an unknown quantity at this point 
Solved Threads: 0
tamereth tamereth is offline Offline
Newbie Poster

c++ homework seg faults

 
0
  #1
Jan 28th, 2008
My program works properly under windows, however, it seg faults when I try to compile/run it under linux.

  1.  
  2.  
  3. #include <fstream>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. char command;
  11. char infilename[80];
  12. char outfilename[80];
  13. char fileline[80];
  14. char nextline[80];
  15. char temp[80];
  16. char temp2[80];
  17. char * location;
  18. char sentence[250];
  19. int loc = 0;
  20. char log[250][80];
  21. int x = 0;
  22. int y = 0;
  23. fstream infile;
  24. fstream outfile;
  25.  
  26. cout << "Enter instructions file name: ";
  27. cin >> infilename;
  28.  
  29. infile.open(infilename, ios::in);
  30. outfile.open("output", ios::out);
  31. if(!infile)
  32. {
  33. cout << "File doesn't exist, exiting.\n";
  34. exit(1);
  35. }
  36.  
  37. infile.getline(sentence, 80);
  38. outfile << sentence << endl;
  39.  
  40. do
  41. {
  42. infile >> command;
  43. infile.getline(fileline, 80);
  44. cout << command << fileline << endl;
  45. if(command == 'I')
  46. {
  47.  
  48. infile >> command;
  49. infile.ignore(10, ' ');
  50. infile.getline(nextline, 80);
  51. cout << command << " " << nextline << endl;
  52.  
  53. if(command=='A')
  54. {
  55. location = strstr(sentence, nextline);
  56.  
  57. location += strlen(nextline);
  58.  
  59. strcpy(temp2, location);
  60. strcpy(location, fileline);
  61. location += strlen(fileline);
  62. strcpy(location, temp2);
  63. location += strlen(temp2);
  64.  
  65. strcpy(temp, "");
  66. x++;
  67. outfile << sentence <<endl;
  68.  
  69. }
  70. else if(command=='B')
  71. {
  72. location = strstr(sentence, nextline);
  73. strcpy(fileline, fileline + 1);
  74. strcat(fileline, " ");
  75. strcpy(temp2, location);
  76. strcpy(location, fileline);
  77. location += strlen(fileline);
  78. strcpy(location, temp2);
  79. location += strlen(temp2);
  80. outfile << sentence << endl;
  81. strcpy(temp, "");
  82. x++;
  83.  
  84.  
  85. }
  86. else if(command != ('A' || 'B'))
  87. {
  88. cout << "Input file corrupt, exiting...\n";
  89.  
  90. }
  91.  
  92. }
  93.  
  94. else if(command == 'R')
  95. {
  96. location = strstr(sentence, fileline);
  97. strcpy(location, (location + strlen(fileline)));
  98. outfile << sentence << endl;
  99. x++;
  100.  
  101. }
  102.  
  103. else
  104. {
  105. cout << "Input file corrupt, exiting...\n";
  106. }
  107.  
  108. }
  109. while(!infile.eof());
  110.  
  111. infile.close();
  112. outfile.close();
  113.  
  114. char readin[80];
  115.  
  116. outfile.open("output", ios::in);
  117.  
  118. while(!outfile.eof())
  119. {
  120. outfile.getline(readin, 80);
  121. cout << readin << endl;
  122. }
  123.  
  124. outfile.close();
  125.  
  126. }

It segfaults after reaching
  1. 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
  1. 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
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,870
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: c++ homework seg faults

 
0
  #2
Jan 28th, 2008
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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: tamereth is an unknown quantity at this point 
Solved Threads: 0
tamereth tamereth is offline Offline
Newbie Poster

Re: c++ homework seg faults

 
0
  #3
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: c++ homework seg faults

 
0
  #4
Jan 28th, 2008
Originally Posted by tamereth View Post
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?

Originally Posted by tamereth View Post
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: tamereth is an unknown quantity at this point 
Solved Threads: 0
tamereth tamereth is offline Offline
Newbie Poster

Re: c++ homework seg faults

 
0
  #5
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,408
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1469
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: c++ homework seg faults

 
0
  #6
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: c++ homework seg faults

 
0
  #7
Jan 28th, 2008
> 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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: tamereth is an unknown quantity at this point 
Solved Threads: 0
tamereth tamereth is offline Offline
Newbie Poster

Re: c++ homework seg faults

 
0
  #8
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 66
Reputation: superjacent is an unknown quantity at this point 
Solved Threads: 3
superjacent's Avatar
superjacent superjacent is offline Offline
Junior Poster in Training

Re: c++ homework seg faults

 
0
  #9
Jan 28th, 2008
Originally Posted by tamereth View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 5
Reputation: tamereth is an unknown quantity at this point 
Solved Threads: 0
tamereth tamereth is offline Offline
Newbie Poster

Re: c++ homework seg faults

 
0
  #10
Jan 28th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC