string input segmentation fault on linux

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

string input segmentation fault on linux

 
0
  #1
Apr 23rd, 2007
c++, Debian linux system, g++ compiler

Alright, I have a serious problem. I keep getting a segmentation fault everytime cin recieves a string with a period in it and stores it into a string. I used couts to find the location of the segmentation fault. it happens EXACTLY after cin >> myString; is called, and only when I input "map.txt". It mighthave something to do with the period directly in the middle. Maybe it'll happen with other characters, but the one I need is a period.

It won't happen if I input "map." , ".txt" , "map..txt" or "hello.txt" but "map.txt" gives a segmentation fault before the-line-after the input. It will happen with either cin >> or getline( cin , myString ).


The reason I need this to work is so I can read in the file in a ifstream In;, using In.open( myString.c_str() ). But I can't do that if it gives me a segmentation fault everytime I input the right name for the file! I just need to know why this isn't working. Any help is appreciated. I might still have some muffins for everyone who helps ^_^
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: string input segmentation fault on linux

 
0
  #2
Apr 23rd, 2007
please post some code
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string input segmentation fault on linux

 
0
  #3
Apr 23rd, 2007
And if you're reading in filenames, don't you think getline() would be a better alternative to cin?
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: string input segmentation fault on linux

 
0
  #4
Apr 23rd, 2007
Infarction: Well, there's not much necessary, but here's the code that gave the error.
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <ifstream>
  5. #include "function.h" /* for the function called, but it's not important. The fault is before it */
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. string fileName;
  11. cout << "What's the file name/path of the map file? ";
  12. cin >> fileName;
  13. /* getline( cin, fileName, '\n' ); does the exact same thing here, I'm afraid, Joe Programmer */
  14. function( fileName );
  15.  
  16. return 0;
  17. }

The segmentation fault happens on the same line it is taken in. Thanks, guys!
Damn computer! It ate everything!
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: string input segmentation fault on linux

 
0
  #5
Apr 23rd, 2007
Does this crash?
  1. int main()
  2. {
  3. string fileName = "map.txt";
  4. function( fileName );
  5. return 0;
  6. }
If yes, you need to post function()

Does this crash?
  1. int main()
  2. {
  3. string fileName;
  4. cout << "What's the file name/path of the map file? ";
  5. cin >> fileName;
  6. return 0;
  7. }
If not, you need to post function()
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: string input segmentation fault on linux

 
0
  #6
Apr 23rd, 2007
Salem: The first one does crash before execution of the line function( fileName );. I tested by putting a cout before the function call. The second doesn't crash.

Alright, well let's see here... I don't like to bother you guys with such long snippets of code... I'll take a look. Is it the peek? Nope... AAAAAAAHHHH! Here you go. It's in a class, so you'll need the class def, class constructor, and main... just use it this way:
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <ifstream>
  5. using namespace std;
  6.  
  7. class MatchingTile
  8. {
  9. public:
  10. MatchingTile( string inFile );
  11.  
  12. private:
  13. vector <vector <string> > board;
  14. vector <vector <string> > visibleBoard;
  15. vector <vector <string> > cpuMemory;
  16. };
  17.  
  18. int main()
  19. {
  20. string fileName;
  21. cout << "What's the file name/path of the map file? ";
  22. cin >> fileName;
  23. MatchingTile Game( fileName );
  24. return 0;
  25. }
  26.  
  27. MatchingTile::MatchingTile( string inFile )
  28. {
  29. ifstream In;
  30. In.open( inFile.c_str() );
  31. while( In.fail() )
  32. {
  33. cout << "\nSorry, but the file failed to open. Try again( Ctrl+C cancels and exits ): ";
  34. cin >> inFile;
  35. In.open( inFile.c_str() );
  36. }
  37.  
  38. string tempLine; // temp string to hold each line
  39.  
  40. vector < vector <string> > backwBoard;
  41. /* A backwards 2-d vector (the rows are the columns in the real one)
  42.   I'm doing this so I can get the size of the board before I create them,
  43.   making it easier to fill them out */
  44.  
  45. while ( !In.eof() ) // starting my readin, it's a bit non-standard so I'll document
  46. {
  47. getline( In, tempLine );
  48. // this getline stores a line from the file into a string
  49.  
  50. string tempString; // temp string to hold each word
  51.  
  52. tempLine.push_back( ' ' ); // add a space to the end of the line for my next getline loop
  53. istringstream inss( tempLine, ios_base::trunc ); /* a string stream that holds the lines
  54.   so I can use nested getlines ^_^ Cool, huh? */
  55.  
  56. vector <string> tempRow; // a temp row for my backwards vector
  57.  
  58. while ( getline( inss, tempString, ' ') ) // get a string from inss, stopping at spaces
  59. {
  60. tempRow.push_back( tempString ); // put it in a row
  61. } // continue until the end of inss
  62.  
  63. backwBoard.push_back( tempRow ); // put the row into the backwBoard
  64. }
  65. In.close();
  66. int row = backwBoard.size(), // store the number of columns in backwBoard as "row"
  67. col = backwBoard[0].size(); // store the no of rows in backwBoard as "col"
  68. // Cause it's backwards! ^_^
  69. vector < string > emptyCol( row , "Down" ); // make an empty column
  70. vector < vector <string> > board;// make boards
  71. vector < vector <string> > visibleBoard;
  72. vector < vector <string> > cpuMemory;
  73. for( int c = 0 ; c < col ; c++ )
  74. {
  75. board.push_back( emptyCol );
  76. visibleBoard.push_back( emptyCol );
  77. cpuMemory.push_back( emptyCol );
  78. }
  79.  
  80. // now to fill out board with the contents of backwBoard
  81. for ( int x = 0 ; x < col ; x++ )
  82. {
  83. for ( int y = 0 ; y < row ; y++ )
  84. {
  85. board[x][y] = backwBoard[y][x];
  86. // backwBoard has columns as rows, so reverse the coordinates from board to get the same
  87. }
  88. }
  89. // constructors don't return anything ^_^
  90. };
I hate bothering people with such long and complicated things... It probably has to do with the vectors. Specifically "board" and "bwBoard" Those are the ones that have the most going on. I'll look into it in the mean time. I tried so hard in those parts to keep from going out, commenting like crazy...

well, thanks for the help, everyone! It would probably take me forever on my own.
Last edited by FireSBurnsmuP; Apr 23rd, 2007 at 7:26 pm. Reason: screwed something up in copy-pastey
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: string input segmentation fault on linux

 
0
  #7
Apr 23rd, 2007
>#include <ifstream>
Hmm, I had to include <fstream> to make this program compile, as well as <vector>.

Even though using getline() doesn't solve your problem, you should still use it. For 2 reasons: a) there's no newline left behind in the input bufer, and b) if the user enters a filename with spaces in it, the you're not left with the first part (the part before the space).

  1. tempLine.push_back( ' ' ); // add a space to the end of the line for my next getline loop
Why? A space at the end of the line isn't going to do any good...

Some other things look strange in your code, although I don't get any seg faults...
Last edited by John A; Apr 23rd, 2007 at 8:13 pm.
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: string input segmentation fault on linux

 
0
  #8
Apr 23rd, 2007
Oh, crap. Yeah I ALWAYS mistype that header, and I guess I did forget the vector library, too. My bad.

I know some things are a bit strange... I will try the getline( cin , fileName ); bit, but you don't get any seg faults? are you on a linux machine? I kinda thought this is a pretty specific problem with linux machines, but I can't find anywhere else where this has happened...

I'll try to figure it out... but everytime I try using debugging statements it makes it appear as though the segmentation fault is occuring exactly as the variable is stored.
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 234
Reputation: FireSBurnsmuP is an unknown quantity at this point 
Solved Threads: 1
FireSBurnsmuP's Avatar
FireSBurnsmuP FireSBurnsmuP is offline Offline
Posting Whiz in Training

Re: string input segmentation fault on linux

 
0
  #9
Apr 24th, 2007
Alright, I managed to figure out that 1) my map files might have extra spaces, causing me to have some columns (lines) that have an empty index.
2) the backwards vector was all I needed, my board is going to be easier to deal with as backwards.
So I was getting segmentation faults because of empty indexes in some columns, and
because I was attempting to do something I no longer understand with the boards. Thanks for helping me bounce ideas, guys. And thanks for the tips, they really do help. ^_^ Now you know why I stick around here.
Damn computer! It ate everything!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
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