943,901 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1181
  • C++ RSS
Jan 28th, 2008
0

c++ homework seg faults

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

c++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tamereth is offline Offline
5 posts
since Jan 2008
Jan 28th, 2008
0

Re: c++ homework seg faults

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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Jan 28th, 2008
0

Re: c++ homework seg faults

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

Re: c++ homework seg faults

Click to Expand / Collapse  Quote originally posted by tamereth ...
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?

Click to Expand / Collapse  Quote originally posted by tamereth ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 28th, 2008
0

Re: c++ homework seg faults

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

Re: c++ homework seg faults

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jan 28th, 2008
0

Re: c++ homework seg faults

> 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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jan 28th, 2008
0

Re: c++ homework seg faults

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

Re: c++ homework seg faults

Click to Expand / Collapse  Quote originally posted by tamereth ...
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?
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
superjacent is offline Offline
66 posts
since Nov 2007
Jan 28th, 2008
0

Re: c++ homework seg faults

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

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Array trouble
Next Thread in C++ Forum Timeline: Hello I need help on a random number generator. PLEASE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC