我有一個由69字節的消息組成的文件。沒有EOL字符 - 只是消息後的消息。該文件中的總字節數正好是11,465,930,307,即(11,465,930,307/69)= 166,172,903個消息。內存映射和排序文件後,字節未被下載
我的程序內存 - 將文件映射到一個字節數組,查看每個69字節的消息並提取時間戳。我跟蹤我在哪個消息編號,然後時間戳和消息編號進入RowDetails
對象,該對象位於std::vector<RowDetails>
,名爲to_sort
,以便我可以通過時間戳有效地對整個文件進行排序。
std::cout << "Sorting....." << to_sort.size() << " rows..." << std::endl;
std::sort(std::begin(to_sort), std::end(to_sort));
然而,然後創建這是排序的新文件:
unsigned long long total_bytes=0;
unsigned long long total_rows=0;
ofstream a_file("D:\\sorted_all");
std::cout << "Outputting " << to_sort.size() << " rows..." << std::endl;
std::cout << "Outputting " << (to_sort.size()*69) << " bytes..." << std::endl;
for(RowDetails rd : to_sort){
for(unsigned long long i = rd.msg_number*69; i<(rd.msg_number*69)+69; i++){
a_file << current_bytes[i];
total_bytes++;
}
total_rows++;
}
std::cout << "Vector rows: "<< total_rows <<std::endl;
std::cout << "Bytes: " << total_bytes <<std::endl;
我的輸出:
No. of total bytes (before memory-mapping file): 11,465,930,307 CORRECT
Sorting....... 166,172,903 rows CORRECT
Outputting 166,172,903 rows.... CORRECT
Outputting 11,465,930,307 bytes CORRECT
Vector rows: 166,172,903 CORRECT
Bytes: 11,465,930,169 ERROR, THIS SHOULD BE 307, not 169
我怎麼可以處理正確的行數,但我的櫃檯,計算總字節數是錯誤的?
當在Windows 7瀏覽器中查看輸出文件時,它說大小:11,503,248,366字節,即使原始輸入文件(我記憶映射)說正確的11,465,930,307。
同意。在輸出中似乎有兩個額外的記錄:11,465,930,307 - 11,465,930,307 = 138; 138 = 69 * 2. –
只是檢查:) – user997112
好吧,它解決了現在我的總字節計數器正確的問題(謝謝)。但是,Windows中輸出文件的大小爲11,502,960,296字節,而輸入文件爲11,465,930,307字節。這是因爲ofstream插入數據? – user997112