fread分段讀取檔案(.txt / .csv)
阿新 • • 發佈:2021-05-02
檔案
Excel中顯示
記事本和Notepad中顯示
程式碼
#include <bits/stdc++.h>
using namespace std;
const char *input_file_name = "input.csv";
const int buf_size = 10*1024*1024; //一次讀10MB
static char buffer[buf_size];
void read(){
FILE* pFILE = fopen(input_file_name, "r");
fseek(pFILE, 0, SEEK_SET );
string str = "";
while(!feof(pFILE)){
int size = fread(buffer, 1, buf_size, pFILE);
char* buf = buffer;
const char* buf_end = buf + size;
while(buf < buf_end){
if (*buf == '\n'){
printf("%s\n", str.c_str());
str = "";
++ buf;
continue;
}
str += *buf, ++buf;
}
}
fclose(pFILE);
return ;
}
int main(){
read();
return 0;
}