2016-08-10 46 views
0

這是我寫的程序,它將diskData.dat文件中的所有行復制到24HrDiskData.dat文件。截至目前,我複製所有行從一個文件到另一個我想最後ñ線從diskData.dat複製到24HrDiskData.dat將最後n行從一個文件寫入另一個文件

#include <iostream> 
#include <stdio.h> 
#include <fstream> 
#include <stdlib.h> 

using namespace std; 

int main(int argc, char *argv[]) { 
    FILE *HrData; 
    char tempData[1024]; 
    int flag = 0; 
    ofstream fout; 

    fout.open("24HrDiskData.dat", ios::app); // open file for appending 
    assert (!fout.fail()); 

    if ((HrData = fopen("/home/xvishuk/diskData.dat", "r")) == NULL) { 
     printf("\nFile cannot be opened"); 
    } 
    while (fgets(tempData, 1024, HrData) != NULL) { 
     fout << tempData; // write the data to the file 
    } 

    return 0; 
} 

樣本模式分隔數據

|Sat Mar 26 18:47:57 2016|1|1|182|60.66|3|30|4782|31|68|4|3467750|110|43.1562|64| 
|Sat Mar 26 19:01:49 2016|1|1|140|46.26|3.03|30|4782|30|68|4|3467764|96|43.1562|64| 
|Sat Mar 26 19:15:40 2016|1|1|184|61.07|3.01|30|4782|30|68|4|3467777|112|43.1562|64| 
|Sat Mar 26 19:29:30 2016|1|1|180|59.91|3|30|4782|32|68|4|3467791|98|43.1562|64| 
|Sat Mar 26 19:43:20 2016|1|1|194|64.61|3|30|4782|32|68|4|3467805|114|43.1562|64| 
|Sat Mar 26 19:57:17 2016|1|1|170|56.62|3|30|4782|30|68|4|3467818|102|43.1562|64| 
|Sat Mar 26 20:11:14 2016|1|1|140|46.32|3.02|30|4782|30|68|4|3467832|118|43.1562|64| 
|Sat Mar 26 20:25:12 2016|1|1|176|58.35|3.02|30|4782|30|68|4|3467846|104|43.1562|64| 
|Sat Mar 26 20:39:10 2016|1|1|202|66.9|3.02|30|4782|30|68|4|3467859|120|43.1562|64| 
|Sat Mar 26 20:53:11 2016|1|1|198|65.85|3.01|30|4782|31|68|4|3467873|106|43.1562|64| 
|Sat Mar 26 21:07:12 2016|1|1|184|60.97|3.02|30|4782|32|68|4|3467887|92|43.1562|64| 
|Sat Mar 26 21:21:11 2016|1|1|152|50.28|3.02|30|4782|31|68|4|3467901|108|43.1562|64| 
|Sat Mar 26 21:35:16 2016|1|1|168|55.77|3.01|30|4782|30|68|4|3467915|94|43.1562|64| 
|Sat Mar 26 21:49:20 2016|1|1|172|57.03|3.02|30|4782|31|68|4|3467928|112|43.1562|64| 
|Sat Mar 26 22:03:26 2016|1|1|152|50.56|3.01|30|4782|33|68|4|3467942|98|43.1562|64| 
|Sat Mar 26 22:17:32 2016|1|1|174|57.86|3.01|30|4782|31|68|4|3467956|114|43.1562|64| 
|Sat Mar 26 22:31:38 2016|1|1|156|51.86|3.01|30|4782|30|68|4|3467970|100|43.1562|64| 
|Sat Mar 26 22:45:44 2016|1|1|202|66.57|3.03|30|4782|30|68|4|3467984|116|43.1562|64| 
|Sat Mar 26 22:59:55 2016|1|1|188|62.4|3.01|30|4782|31|68|4|3467998|102|43.1562|64| 
|Sat Mar 26 23:14:06 2016|1|1|164|53.95|3.04|30|4782|32|68|4|3468012|118|43.1562|64| 
|Sat Mar 26 23:28:17 2016|1|1|168|55.78|3.01|30|4782|31|68|4|3468026|104|43.1562|64| 
|Sat Mar 26 23:42:28 2016|1|1|176|58.33|3.02|30|4782|30|68|4|3468040|120|43.1562|64| 
|Sat Mar 26 23:56:39 2016|1|1|170|55.76|3.05|30|4782|30|68|4|3468053|106|43.1562|64| 
|Sun Mar 27 00:10:54 2016|1|1|172|56.97|3.02|30|4782|31|68|4|3468067|92|43.1562|64| 
|Sun Mar 27 00:25:13 2016|1|1|184|61.23|3.01|30|4782|33|68|4|3468081|108|43.1562|64| 
|Sun Mar 27 00:39:26 2016|1|1|188|62.12|3.03|30|4782|31|68|4|3468096|94|43.1562|64| 
|Sun Mar 27 00:53:42 2016|1|1|170|56.42|3.01|30|4782|30|68|4|3468110|110|43.1562|64| 
|Sun Mar 27 01:07:58 2016|1|1|174|57.8|3.01|30|4782|31|68|4|3468124|96|43.1562|64| 

預期輸出

最近10條記錄。

|Sat Mar 26 22:45:44 2016|1|1|202|66.57|3.03|30|4782|30|68|4|3467984|116|43.1562|64| 
|Sat Mar 26 22:59:55 2016|1|1|188|62.4|3.01|30|4782|31|68|4|3467998|102|43.1562|64| 
|Sat Mar 26 23:14:06 2016|1|1|164|53.95|3.04|30|4782|32|68|4|3468012|118|43.1562|64| 
|Sat Mar 26 23:28:17 2016|1|1|168|55.78|3.01|30|4782|31|68|4|3468026|104|43.1562|64| 
|Sat Mar 26 23:42:28 2016|1|1|176|58.33|3.02|30|4782|30|68|4|3468040|120|43.1562|64| 
|Sat Mar 26 23:56:39 2016|1|1|170|55.76|3.05|30|4782|30|68|4|3468053|106|43.1562|64| 
|Sun Mar 27 00:10:54 2016|1|1|172|56.97|3.02|30|4782|31|68|4|3468067|92|43.1562|64| 
|Sun Mar 27 00:25:13 2016|1|1|184|61.23|3.01|30|4782|33|68|4|3468081|108|43.1562|64| 
|Sun Mar 27 00:39:26 2016|1|1|188|62.12|3.03|30|4782|31|68|4|3468096|94|43.1562|64| 
|Sun Mar 27 00:53:42 2016|1|1|170|56.42|3.01|30|4782|30|68|4|3468110|110|43.1562|64| 
+2

您是否知道'tail'程序已經存在這種確切的行爲? –

+0

發佈的期望輸出是**不是** las 10記錄? – chqrlie

+0

謝謝大家! @BenVoigt我同意,但我需要使用C++程序 – user1885183

回答

4

要只寫最後n行,你可以分配n字符串數組和行存儲到它,你看他們,只保留最後n當數組填滿,採用模塊化的指數,以避免多餘的複製。

當您到達文件末尾時,輸出數組中的行。

還避免混合C和C++。可以使用<stdio.h><iostream>,但同時使用兩者都容易出錯並且不夠優雅。

這是在C簡單的實現:

#include <errno.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define N 100 

int main(void) { 
    char line[1024]; 
    char *stash[N] = { NULL }; 
    int i, j; 
    FILE *HrData, *fout; 

    if ((fout = fopen("24HrDiskData.dat", "a")) == NULL) { 
     fprintf(stderr, "cannot open 24HrDiskData.dat for appending: %s\n", 
       strerror(errno)); 
     return 1; 
    } 
    if ((HrData = fopen("/home/xvishuk/diskData.dat", "r")) == NULL) { 
     fprintf(stderr, "cannot open input file: %s\n", 
       strerror(errno)); 
     return 1; 
    } 
    for (i = 0; fgets(line, 1024, HrData) != NULL;) { 
     free(stash[i]); 
     stash[i] = strdup(line); 
     i = (i + 1) % N; 
    } 
    fclose(HrData); 
    for (j = i;;) { 
     if (stash[i]) 
      fputs(stash[i], fout); 
     i = (i + 1) % N; 
     if (i == j) 
      break; 
    } 
    fclose(fout); 
    return 0; 
} 
+0

隨機複製了10行出於好奇,爲什麼你需要將行存儲在內存中?是否有足夠的內存來存儲這些行? –

+0

@ThomasMatthews:將行存儲在內存中是最簡單的方法。即使輸入文件是設備或管道,它也能正常工作:尋找輸入文件是不可能的。現代計算機有足夠的內存來存儲'n'和輸入文件類型的大部分合理值。 – chqrlie

+0

更好的方法是使用'std :: string',因爲它動態分配。你可以在'std :: ifstream'中使用'std :: getline'。 –

2

兩個通行證。

第一遍,計算行數並存儲它們的文件位置。

第二遍,計算想要開始複製的行號。在容器中查找它的文件位置。尋找文件位置。開始複製。

編輯1:實施例的代碼

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 

int main(void) 
{ 
    std::string text_line; 
    std::vector<std::streampos> file_positions; 
    std::ifstream input_file("my_file.txt"); 
    file_positions.push_back(input_file.tellg()); 
    while (std::getline(input_file, text_line)) 
    { 
     file_positions.push_back(input_file.tellg()); 
    } 
    // The total lines is file_positions.size(). 
    // Return the last 13 lines 
    const unsigned int total_lines = file_positions.size(); 
    std::streampos seek_position = 0; 
    unsigned int index = 0U; 
    if (total_lines > 13U) 
    { 
     index = total_lines - 13U; 
    } 
    input_file.clear(); 
    input_file.seekg(file_positions[index]); 
    // Copy text lines to output file... 
    return 0; 
} 

編輯2 - 更有效的方法
你通過文件讀你可以維持的Ñ文本行的容器。

#include <deque> 
//... 
int main(void) 
{ 
    std::deque<std::string> text_lines(N); 
    while (std::getline(input_file, text_line)) 
    { 
    if (text_lines.size() == N) 
    { 
     text_lines.pop_front(); 
    } 
    text_lines.push_back(text_line); 
    } 
    // Now copy the text lines from the `std::deque` to the output file. 
    //... 
    return 0; 
} 
+0

爲什麼使用兩遍?爲什麼不能一次完成所有操作,因爲您必須逐行讀取文件。 – NathanOliver

+1

一,你不知道文件中有多少行。其次,可能沒有足夠的內存來容納整個文件。 –

+0

如果輸入文件是管道或設備,或者在回收時修改了輸入文件,那麼這將不起作用,因爲很可能是日誌文件(如OP正在讀取的文件)的情況。 – chqrlie

相關問題