linux下C函数---fscanf和fprintf的使用

时间:2021-12-05 23:16:32

//头文件:

#include <stdio.h>

#include <string.h>

const long long max_size = 2000;  

int main(int argc, char **argv) {

//文件读取
FILE *f;

char file_name[max_size];

long long words, size;


strcpy(file_name, argv[1]);

f = fopen(file_name, "rb");

if (f == NULL) {

printf("Input file not found\n");

return -1;

}

fscanf(f, "%lld", &words);

fscanf(f, "%lld", &size);


//文件输出

FILE *f2;

char result[max_size];

strcpy(result,argv[3]);

strcat(result,"/");

strcat(result,argv[2]);

//先清空文件

f2=fopen("result", "w");

fclose(f2);

//然后往文件以“加到尾部”方式写入数据

f2=fopen("result", "ab+");

for (a = 0; a < N; a++){

printf("%50s\t\t%f\n", bestw[a], bestd[a]);

fprintf(f2,"%50s\t\t%f\n", bestw[a], bestd[a]);

}

fclose(f2);

return 0;

}