在C ++中将字符串保存到文本文件

时间:2015-01-19 05:14:35

标签: c++-cli

我有错误,我无法解决......

我想一起构建配置字符串并保存到文本文件。但我总是得到'System.Runtime.InteropServices.SEHException'类型的异常。 此外,如果文本文件已经存在,我该如何覆盖。

谢谢

该功能如下所示:

             // get dialog title
             String^ s = this->groupBox1->Text;
             std::string dialogName = marshal_as<std::string>(s);
             const char * dialogTitle = dialogName.c_str();

             // get list mit DBC Dateinamen
             int numberOfDbcFile = listView1->Items->Count;
             std::string nameStr = "";

             for(int i =0; i< numberOfDbcFile;i++)
             {
                 System::Windows::Forms::ListViewItem^  listViewItem = this->listView1->Items[i];
                 String^ fileNr = listViewItem->SubItems[0]->Text;
                 String^ fileName = listViewItem->SubItems[1]->Text;
                 String^ filePath = listViewItem->SubItems[2]->Text;
                 nameStr += marshal_as<std::string>(fileNr)+" ";
                 nameStr += marshal_as<std::string>(fileName)+" ";
                 nameStr += marshal_as<std::string>(filePath)+"\n";
             }

             char *fileList = new char(strlen(nameStr.c_str()) +1);
             /*strcpy(fileList, nameStr.c_str());*/
             if(fileList == NULL){
                 /*throw bad_alloc();*/
                 cerr << "Fehler" << endl;
                 exit(1);
                 /*strcpy(fileList, nameStr.c_str());*/
           } else {
                 /*fileList = NULL;*/ 
                 strcpy(fileList, nameStr.c_str());
             }
             // get length of list
             int lengthOflist = sizeof(fileList);  

             // get cfg string
             int checkList = listView2->Items->Count;
             int checkList3 = listView3->Items->Count;
             int a = 0;
             std::string cfgS = "";
             for(int i =0; i< checkList;i++)
             {
                 if(listView2->Items[i]->Checked && listView2->Items[i]->Selected == true)
                 {
                    String^ dbc =  label2->Text;
                    cfgS += "#" + marshal_as<std::string>(dbc) + bo_name +"\n";
                    System::Windows::Forms::ListViewItem^  listViewItem = this->listView2->Items[i];
                    String^ boid = listViewItem->SubItems[0]->Text;
                    String^ bolaenge = listViewItem->SubItems[2]->Text;
                    String^ boN = listViewItem->SubItems[1]->Text;
                    String^ bokom = textBox1->Text;
                    std::string boStr = "@" + marshal_as<std::string>(boid)+":";
                    boStr += marshal_as<std::string>(bolaenge)+":";
                    boStr += marshal_as<std::string>(boN)+":";
                    boStr += "'"+ marshal_as<std::string>(bokom)+"'"+"\n";
                    cfgS += boStr;
                    a++;
                    for(int j =0; j< checkList3;j++)
                    {
                         if(listView3->Items[j]->Checked)
                         {
                            System::Windows::Forms::ListViewItem^  listViewItem = this->listView3->Items[j];
                            String^ sgName = listViewItem->SubItems[0]->Text;
                            String^ sgBit = listViewItem->SubItems[1]->Text;
                            String^ sgLaenge = listViewItem->SubItems[2]->Text;
                            String^ sgFaktor = listViewItem->SubItems[3]->Text;
                            String^ sgOffset = listViewItem->SubItems[4]->Text;
                            String^ sgMin = listViewItem->SubItems[5]->Text;
                            String^ sgMax = listViewItem->SubItems[6]->Text;
                            String^ sgEinheit = listViewItem->SubItems[7]->Text;
                            String^ sgkom = Kommentar->Text;
                            std::string sgStr = "|"+ marshal_as<std::string>(sgName)+":";
                            sgStr += marshal_as<std::string>(sgBit)+":";
                            sgStr += marshal_as<std::string>(sgLaenge)+":";
                            sgStr += marshal_as<std::string>(sgFaktor)+":";
                            sgStr += marshal_as<std::string>(sgOffset)+":";
                            sgStr += marshal_as<std::string>(sgMin)+":";
                            sgStr += marshal_as<std::string>(sgMax)+":";
                            sgStr += marshal_as<std::string>(sgEinheit) + ":";
                            sgStr += "'"+ marshal_as<std::string>(sgkom)+"'"+"\n";
                            cfgS += sgStr;
                         }
                    }
                 }  
             }


             cfgS += "##";


             char *cfgString = new char(strlen(cfgS.c_str()) +1);
             if(cfgString == NULL){
                 cerr << "Fehler" << endl;
                 exit(1);
                 /*throw bad_alloc();*/

           } else {
                 /*cfgString = NULL; */
                 strcpy(cfgString, cfgS.c_str());
             }
             // get cfg string length
             int lengthOfString = sizeof(cfgString);
             // get max number of selected BO
             maxNumOfSelection = a;

             // call configuration function
            int32_t result = m_OpenBusConfigurationDialog->openBusConfigurationDialog(
                    dialogTitle,fileList, lengthOflist,cfgString,lengthOfString, maxNumOfSelection);

            ofstream fichier("C:\\Users\\akoudjou\\Desktop\\CAN Import\\Test2\\test.txt",ios::out); 
              if(fichier.is_open()){
             fichier << fileList << endl;
             fichier << cfgString << endl;
             fichier.close();
              }
              else{
                  cout << "Datei konnte nicht geöffnet werden" << endl;
              }
             exit(0);

         }

1 个答案:

答案 0 :(得分:0)

这可能是您遇到的错误,也可能不是,但肯定是错误。

char *fileList = new char(strlen(nameStr.c_str()) +1);

分配一个char,用一个等于nameStr + 1的长度的整数值初始化它。但是你实际上是将它用作数组,所以使用方括号初始化它:

char *fileList = new char[strlen(nameStr.c_str()) +1];
                      // ^                          ^
                      // |     Square brackets      |

你以后再做一次:

char *cfgString = new char(strlen(cfgS.c_str()) +1);