fread不分段读取文件(.txt / .csv)

文件

在Excel中显示
fread不分段读取文件(.txt / .csv)_第1张图片
在记事本和Notepad中显示
fread不分段读取文件(.txt / .csv)_第2张图片

代码

#include 
using namespace std;

const char *input_file_name = "input.csv";

void read(){
  FILE* pFILE = fopen(input_file_name, "r");
  fseek(pFILE, 0, SEEK_END);
  int file_size = ftell(pFILE);
  char* buf = new char[file_size];
	fseek(pFILE, 0, SEEK_SET);
  fread(buf, 1, file_size, pFILE);
  fclose(pFILE);
  string str = "";
	for(int i = 0; i < file_size; ++i){
    if (buf[i] == '\n'){
      printf("%s\n", str.c_str());
      str = "";
      continue;
    }
    str += buf[i];
	}
  delete[] buf;
  return ;
}

int main(){
  read();
  return 0;
}

你可能感兴趣的:(IO)