gpt4 book ai didi

c++ - 处理指针 c++ 的段错误(这次有更多代码)

转载 作者:行者123 更新时间:2023-11-28 07:14:05 25 4
gpt4 key购买 nike

我希望有人能帮助我解决我遇到的段错误,这次我发布了更多代码,希望每个人都能看到我正在尝试做什么。我确信这与我如何指向所有内容有关,因为这是我遇到麻烦的部分。这是.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);

这是.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);
}

和 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;

}

最后,输出:

     $ ./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)

最佳答案

因此,根据对您问题的评论,我建议了解调试器或习惯插入跟踪语句(例如 cout << "got here."; function(); cout << "got here以及!”;等)。

在这种情况下,看起来 question* questions[argc-1] 会咬你一口。它只是创建指向 question 的指针数组,而不是 question 的数组。

您可以按需分配这些(不要忘记事后清理)或使用一些可以更干净地管理生命周期的东西(查看 std::vector 以获得具有其他一些有用特征的基本数组替换).

开始分配结构后,您会注意到您还没有在结构中分配 programQuestion。可能是时候考虑将其作为一个类,或者看看 std::string 的工作方式了。您可以很容易地从 C 字符串(指向 char 的指针)进行初始化。请参阅:Converting a C-style string to a C++ std::string

关于c++ - 处理指针 c++ 的段错误(这次有更多代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20509147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com