user2990286
user2990286

Reputation: 139

Segmentation fault dealing with pointers c++ (more code this time)

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)

Upvotes: 0

Views: 88

Answers (1)

chaboud
chaboud

Reputation: 736

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

Upvotes: 1

Related Questions