2012-02-08 92 views
0

我正在直接回答這個問題。我通過逐行讀取文件併合並字符串來完成,但需要一個很好的解釋來說明如何使用矢量來完成。我的目標是使用系統命令(Windows XP,7)來檢查目錄內容,並將它們直接發送到'vector sysoutp'作爲示例,然後將其與第二個文件中的信息合併到新文件中。有任何想法嗎?如何在另一個或文件中使用矢量合併兩個或多個流?

只是爲了我的代碼示例部分:

while (getline(myinputfile, line)) 
    { 
     lines++; 
     found = line.find(countchars); 
     if (found!=string::npos) 
     { 
      line.erase(int(found) + 4); 
      myoutputfile << line << endl; 
     } 
    } 

其中myinputfile充滿了「syscomm」,這個想法是用向量轉換但顯然,當我試圖做類似getline(cin, tmp)其中「CIN」是syscomm沒有任何反應(當然,錯誤彈出):(

我也有和使用該preproccessor指令

#define syscomm system("dir /b | find /i /v ".exe .txt")時出現錯誤:

createlist.cpp|15|warning: missing terminating " character [enabled by default]| 
createlist.cpp|53|error: missing terminating " character| 
createlist.cpp||In function 'int main()':| 
createlist.cpp|53|error: request for member 'exe' in '"dir /b | find /i /v "', which is of non-class type 'const char [21]'| 
||=== Build finished: 2 errors, 1 warnings ===| 

它沒有添加「|」找到......」,但使用它,我可以過濾不需要的文件名與他們已知擴展。

謝謝合作和遺憾,2合1的問題包:)

+0

一個問題,請。 – 2012-02-08 11:19:38

+0

那麼,什麼是讀一行在命令輸出線的基本算法,並在它們插入以下是這個向量rt行動?我知道怎麼做,但閱讀很難理解... Did: 'while(!inputcomm.eof()) { sortedvector.push_back(inputcomm.getline(buffer,10)); } sort(sortedvector.begin(),sortedvector.end()); pclose(inputcomm); return sortedvector;' 其中inputcomm爲例如'FILE * inputcomm = popen(「dir/b | findstr/i \」\\ <*。sd \「」,「rt」);' :( – 1000Gbps 2012-02-12 08:45:04

回答

0

檢查#define再次...

#define syscomm system("dir /b | find /i /v ".exe .txt") 
// String starts here -^     ^
// String ends here ------------------------' 

應該在你的編輯器是非常明顯的,如果它有語法着色。

同時
+0

,我的錯誤,忘記關閉引號,但仍輸出exec和txt文件名:| – 1000Gbps 2012-02-08 12:51:01

+0

@ 1000Gbps字符串中的雙引號必須轉義,如下所示:''string \「contains \」four \「double \ 「quotes」''你也這麼做了嗎? – 2012-02-08 13:11:06

+0

所以我必須使用'「dir/b | findstr/i/v \」。exe .txt「」?'我忘了'find'不能處理兩個以上的字符串。必須使用'findstr',現在它工作:) – 1000Gbps 2012-02-08 13:36:43

相關問題