2013-12-11 227 views
0

我想這個問題已被問過一百萬次,但我無法在任何地方找到答案。應該很簡單。好了,所以我有一個結構在我的.h文件:將char *轉換爲char? C++

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

而在h和論據,我已啓動功能中的.cpp:

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

} 

確定一切正常爲止。現在主要我有嘗試存儲問題[1] programQuestion的argv:

char* fileName = argv[count+1]; 

依次爲:

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

我與指針不是真正的好,所以如果有人可以幫助我店將char * argv放入char問題[count] .programQuestion中將會非常棒。謝謝!

+5

這是不可能的。 'char'是一個字符。一個字符串是很多字符。回到你正在學習的書,看看它的一些字符串處理的例子。 –

+0

那麼爲什麼'cout << filename << endl;'在屏幕上打印「q1.txt」?對我來說,看起來像6個字符 – user2990286

+0

從char *到char的最明顯的方法是簡單地解引用char *(即將星號放在前面)。但不知何故,我懷疑這實際上是想要的。 –

回答

0

更改programQuestioncharchar*

而且,這個功能

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

} 

我覺得應該是

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

幾乎不存在,只有1個字符的文件名的文件。

+0

我試過,但由於某種原因,程序在到達調用setQuestionFileName的部分時會中斷。文件名永遠不會被打印。 – user2990286

+0

將簽名更改爲'setQuestionFileName(question * q,char fileName)'。看到我對這個問題的評論。 –

+0

當簡單地指定指針或'char fileName [SOME_VALUE];'時,'struct question'必須調整爲使用'char const * fileName;'。 –

0

我打算髮布代碼,我認爲這樣可能會更好。這是.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; 

} 

,現在是這樣的,我得到一個分段錯誤。