1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
|
//采用C模式对Txt进行读取 void TxtRead_Cmode() {
FILE * fid = fopen("txt_out.txt","r"); if(fid == NULL) {
printf("打开%s失败","txt_out.txt"); return; } vector<<span style="color:#8000ff;">int> index; vector<<span style="color:#8000ff;">double> x_pos; vector<<span style="color:#8000ff;">double> y_pos; int mode = 1; printf("mode为1,按字符读入并输出;mode为2,按行读入输出;mode为3,知道数据格式,按行读入并输出\n"); scanf("%d",&mode); if(mode == 1) {
//按字符读入并直接输出 char ch; //读取的字符,判断准则为ch不等于结束符EOF(end of file) while(EOF!=(ch= fgetc(fid))) printf("%c", ch); } else if(mode == 2) {
char line[1024]; memset(line,0,1024); while(!feof(fid)) {
fgets(line,1024,fid); printf("%s\n", line); //输出 } } else if(mode == 3) {
//知道数据格式,按行读入并存储输出 int index_tmp; double x_tmp, y_tmp; while(!feof(fid)) { fscanf(fid,"%d%lf%lf\n",&index_tmp, &x_tmp, &y_tmp); index.push_back(index_tmp); x_pos.push_back(x_tmp); y_pos.push_back(y_tmp); } for(int i = 0; i < index.size(); i++) printf("d\t%4.8lf\t%4.8lf\n",index[i], x_pos[i], y_pos[i]); } fclose(fid); } |