0

I was hoping someone could help me with a segmentation fault I'm getting, I'm posting more code this time in hopes everyone can see what I'm trying to do. I'm sure it has to do with how I point to everything as that's the part that I'm having trouble with. This is the .h

  using namespace std;
  // Data

  struct question{
  string programNum;
  string programDesc;
  string programPoints;
  string programInput;
  char* programQuestion;
  };

  void setQuestionFileName(question* q, char* fileName);
  void display(question* q);
  void display(question* q);

This is the .cpp

using namespace std;


void setQuestionFileName(question* q, char* fileName){
    strcpy(q->programQuestion, fileName);
}

void display(question* q){

       cout << "Description           =  "   << q->programDesc     << endl;
       cout << "Number of Points      =  "   << q->programPoints   << endl;
       cout << "Name of Question File =  "   << q->programQuestion << endl;

}

// Not used or tested yet
int myCompare (const void * a, const void * b ) {
           const char *pa = *(const char**)a;
           const char *pb = *(const char**)b;

           return strcmp(pa,pb);
 }

And main.cpp:

 using namespace std;

 int main(int argc, char* argv[]){                                                              //or char** argv
 question* questions[argc-1];                                                           //Array of questions to be filled by loop.
 int sizeOfQuestions = argc;                                                                    //number of questions passed in at run time
 int numLines = 0;                                                                          //number of lines in file
 for(int i=0;i<argc;i++){                                                                   //Test loop to make sure the command line file names are read in
          std::cout << argv[i] << " says hello" << std::endl;
 }
 for(int count=0;count<sizeOfQuestions-1;count++){                                          //This loop places the information from the files into structs
      //char fileName = argv[count+1];
      char* fileName = argv[count+1];
      cout << "Problem number:  " << count+1 << "\t Working with file " << fileName << endl;
      std::fstream questionFile (fileName, std::fstream::in);                                    //Open the file
      if(questionFile.good()){
            cout << "File Opened" << endl;
            setQuestionFileName(questions[count],fileName);
            cout << questions[count]->programQuestion << endl;
            getline(questionFile,questions[count]->programNum);
            getline(questionFile,questions[count]->programDesc);
            getline(questionFile,questions[count]->programPoints);
            getline(questionFile,questions[count]->programInput);
            display(questions[count]);
            questionFile.close();
       }else{
            cout << "Could not open file!!!" << endl;
       }

  }

  return 0;

  }

And lastly, the output:

     $ ./a.exe q1.txt q2.txt
     ./a says hello
     q1.txt says hello
     q2.txt says hello
     Problem number:  1       Working with file q1.txt
     File Opened
     Segmentation fault (core dumped)
user2990286
  • 139
  • 2
  • 10
  • 1
    Use your debugger. If you don't know how to use a debugger, now is the time to learn. – David Hammen Dec 11 '13 at 02:00
  • 2
    `question* questions[argc-1];` allocates space for pointers to question structs, but you never actually allocate the structs themselves, so later when you try and read data into them it crashes because it's going into random memory you don't own. Also allocating an array like that isn't standard, so it may work on your compiler but it won't work on all of them. – Retired Ninja Dec 11 '13 at 02:03
  • 1
    Try this: `std::vector questions;` in your `main()` function. – Thomas Matthews Dec 11 '13 at 02:04
  • ...and `strcpy(q->programQuestion, fileName);` will crash because you've declared `programQuestion` as a `char*` (without allocating any memory) instead of `std::string` like the other members. – Blastfurnace Dec 11 '13 at 02:05
  • Alright that makes sense. Thank you. Somehow its crashing after the two `qx says hello` statements now. – user2990286 Dec 11 '13 at 02:06
  • 1
    Not sure why `programQuestion` isn't a string like the rest of the members. It should be, and as @ThomasMatthews said, use vector to store the structs. – Retired Ninja Dec 11 '13 at 02:07

1 Answers1

1

So, as per the comments on your question, I'd suggest getting to know a debugger or getting used to inserting tracing statements (e.g. cout << "got here."; function(); cout << "got here as well!";, etc).

In this case, it looks like question* questions[argc-1] is going to bite you. It's just creating an array of pointers to questionrather than an array of question.

You could either allocate these on demand (don't forget to clean up afterwards) or use something that will manage lifetime for you a little more cleanly (look at std::vector for a basic array replacement with some other useful characteristics).

After you start allocating your struct, you'll notice that you also haven't allocated your programQuestion within your struct. It might be time to look at making this a class, or take a look at the way that std::string works. You can initialize from a C string (pointer to char) pretty easily. See: Converting a C-style string to a C++ std::string

Community
  • 1
  • 1
chaboud
  • 736
  • 5
  • 4