预读(读取文件前几行)文件(txt,dat,csv等)程序
需求:
txt、dat、csv文件很大,需要花很长的时间打开,
但实际上我们只需要查看文件的前几行,查看数据的内容和格式
exe & code : https://github.com/congmingyige/preRead-first-few-lines-txt-dat-csv-
展示:
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <io.h> 5 #include <windows.h> 6 using namespace std; 7 8 char addr[200],s[10000],str[100000]; 9 10 int main() 11 { 12 printf("preRead!\n"); 13 printf("Please input (directory+)filename:\t"); 14 ///data.dat 15 ///test.txt 16 ///1.csv 17 ///1.xlsx wrong! 18 ///1.doc wrong! 19 gets(addr); 20 if (_access(addr,0)!=-1) 21 { 22 int i,limitLine; 23 24 FILE *in_line=fopen("limitLine.txt","r"); 25 fscanf(in_line,"%d",&limitLine); 26 fclose(in_line); 27 28 FILE *in=fopen(addr,"r"); 29 ///若不清楚格式,按行读取 30 i=1; 31 strcpy(str,""); 32 while (fgets(s,1000,in)!=NULL) 33 { 34 strcat(str,s); 35 if (i==limitLine) 36 break; 37 i++; 38 } 39 printf("%s",str); 40 fclose(in); 41 } 42 else 43 printf("File not exists\n"); 44 system("pause"); 45 return 0; 46 }