2014-09-30 45 views
0

我有一個代碼,我希望它從命令行獲得輸入文件,並在最後創建輸出文件與XXX - 意思是如果intput =「blabla.txt」或「/ johny/first/blabla.txt「我直到得到」blablaXXX.txt「或」/johny/first/blablaXXX.txt「複製字符串直到'。'以及如何只複製當我知道結構

第二個問題是,當我找到一條線我正在尋找我想只複製數字(保持時間模式)和len個

線將「在此時間12:04:56.186,LEN 000120」

,我想在新文件一線得到:12:04 :56.186 120

#include <iostream> 
#include <fstream> 

using namespace std; 

int main(int argc, char* args[]) 
{ 
    string inputName=args[1]; 

    ifstream inputName(inputFileName); 

    ////// here i will need to get the output string name some thing like 
    // string outputFileName=EDITED_INPUT_NAME+"XXX"+".txt"; 

    ofstream outpuName(outputFileName); 

     while(std::getline(inputName, line)) 
     { 
       if(line.find("IT IS HERE") != string::npos) 
        // how to make it take only the parts i need?????? 
        outpuName << line << endl; 
       cout << line << endl; 
     } 


    inputName.close(); 
    outpuName.close(); 
    return 0; 
} 

回答

1

這是否解決您的問題:

#include <iostream> 
#include <fstream> 
#include <cstdlib> 

using namespace std; 

int main(int argc, char* args[]) { 
    ifstream inputFile(args[1]); 
    // Your first problem 
    string outputFileName(args[1]); 
    outputFileName.insert(outputFileName.find("."), "XXX"); 
    cout << "Writing to " << outputFileName << endl; 
    // End of first problem 

    ofstream outputFile(outputFileName.c_str()); 
    string line; 

    while (getline(inputFile, line)) { 
     if (line.find("IT IS HERE") != string::npos) { 
      // Your second problem 
      string::size_type time_start = line.find("time ") + 5; 
      string::size_type time_end = line.find(",", time_start); 
      cout << time_start << " " << time_end << endl; 
      string time = line.substr(time_start, time_end - time_start); 

      string::size_type len_start = line.find("len ") + 4; 
      string::size_type len_end = line.find(" ", len_start); 
      if (len_end != string::npos) 
       len_end += 4; 
      int len = atoi(line.substr(len_start, len_end - len_start).c_str()); 
      // End of second problem 

      outputFile << time << " " << len << endl; 
      cout << time << " " << len << endl; 
     } 
    } 
    inputFile.close(); 
    outputFile.close(); 
    return 0; 
} 

例輸入:

sdfghjk sdfghjk fghjkl 
IT IS HERE time 12:04:56.186, len 000120 
usjvowv weovnwoivjw wvijwvjwv 
IT IS HERE time 12:05:42.937, len 000140 

輸出示例:

12:04:56.186 120 
12:05:42.937 140 

的代碼可能看起來與std::regexauto更好,但因爲這沒有用標記,我阻止了。