将char *转换为char? C ++

时间:2013-12-11 01:09:50

标签: c++ pointers

我认为这个问题已被问过一百万次,但我无法在任何地方找到答案。应该很简单。好的,我的.h文件中有一个结构:

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

然后我在.h和.cpp中的参数中启动了函数:

void setQuestionFileName(question q, char fileName){
    q.programQuestion = fileName;

}

好的,到目前为止一切顺利。现在主要是我有尝试在programQuestion中存储argv [1]的问题:

char* fileName = argv[count+1];

接下来是:

setQuestionFileName(questions[count],fileName);
cout << questions[count].programQuestion << endl;

我对指针并不是很好,所以如果有人能帮我将char * argv存储到char问题[count] .programQuestion那将是惊人的。谢谢!

2 个答案:

答案 0 :(得分:1)

我要发布代码,我认为它可能会更好用。这是.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;

}

现在的方式,我遇到了分段错误。

答案 1 :(得分:0)

programQuestionchar更改为char*

而且,这个功能

void setQuestionFileName(question q, char fileName){
    q.programQuestion = fileName;

}

我认为应该是

void setQuestionFileName(question& q, char* fileName){
    strcpy(q.programQuestion, fileName);
}

几乎没有一个只有1个字符的文件。