#include
#define MAX 256
#define NUM 9
int main(void) {
FILE* tfp = fopen("C:\\Users\\Desktop/999.trs", "r");
if (tfp == NULL) {
printf("打开文件失败!\n");
return 0;
}
char temp[MAX];
int id;
double message;
double result[NUM];
double data[NUM];
int i = 0;
FILE* fp = fopen("C:\\Users\\Desktop/excelTest.csv", "w");
if (fp == NULL) {
printf("打开文件失败!\n");
return 0;
}
fprintf(fp, "第一个数据,第二个数据,第三个数据,第四个数据\n");
while (feof(tfp) == 0) {
if (i == 0) {
fgets(temp, MAX, tfp);
fgets(temp, MAX, tfp);
fgets(temp, MAX, tfp);
}
//读走前三列,包括起始的"<" 这一列
fscanf(tfp, "%s", temp);
fscanf(tfp, "%s", temp);
fscanf(tfp, "%d", &id);
//读取目标列的数据
//https://baike.baidu.com/item/fscanf/10942374?fr=aladdin
int num = fscanf(tfp, "%lf", &data[i]);
if (num == 1) {
printf("message=%f\n", data[i]);
i++;
if (i == NUM) {
for (int j = 0; j < NUM; j++) {
result[j] = data[j];
printf("第一行为:%f\n", result[j]);
}
//写入excel文件
//https://zhuanlan.zhihu.com/p/464082154
//https://blog.csdn.net/hongzhen91/article/details/57422897/
int k;
for (k = 0; k < 0; k++) {
fprintf(fp, "%f,", result[k]);
}
fprintf(fp, "%f\n", result[k]);
i = 0; //开始读取下面新的一组10行数据
}
}
//读取后面六列的数据
fscanf(tfp, "%lf", &message);
fscanf(tfp, "%lf", &message);
fscanf(tfp, "%lf", &message);
fscanf(tfp, "%d", &id);
fscanf(tfp, "%lf", &message);
fscanf(tfp, "%s\n", temp);
}
fclose(tfp);
fclose(fp);
return 0;
}
你得把输入文件trs的文件格式和输出文件csv的文件格式列出来,别人才好帮你修改程序
每读取四列数据10组就重新读走开始的三行即可,修改如下:
参考链接:
C语言中fscanf函数读取double型浮点数的问题_fancentury的博客-CSDN博客_fscanf 浮点数
#include <stdio.h>
#define MAX 256
#define NUM 10
struct datas{
int first;
double second;
double third;
double fourth;
};
int main(void){
FILE * tfp = fopen("lplplp.txt","r");
if(tfp==NULL){
printf("打开文件失败!\n");
return 0;
}
char temp[MAX];
int id;
double message;
// double result[NUM]; //每一行与第一行第三列的互差存入这个数组
struct datas result; //存储每一行目标数据列的 数据
int i=0;
FILE * fp = fopen("excelTest2.csv","w");
if(fp==NULL){
printf("打开文件失败!\n");
return 0;
}
fprintf(fp,"第一个数据,第二个数据,第三个数据,第四个数据\n");
while (feof(tfp)==0){
if(i==0){ //读取每组数据开始时,读走前面的三行
//https://baike.baidu.com/item/feof/10942186?fr=aladdin
//https://zhuanlan.zhihu.com/p/150698454
//先文件的前三行读走
fgets(temp,MAX,tfp);
fgets(temp,MAX,tfp);
fgets(temp,MAX,tfp);
}
//读走前两列,包括起始的"<" 这一列
fscanf(tfp,"%s",temp);
fscanf(tfp,"%s",temp);
int num=fscanf(tfp,"%d",&result.first);
if(num==1){
//https://blog.csdn.net/qq_41430551/article/details/90409644
fscanf(tfp,"%lf",&result.second);
fscanf(tfp,"%lf",&result.third);
fscanf(tfp,"%lf",&result.fourth);
//把四列数据写入csv文件
//https://blog.csdn.net/geng823/article/details/41015283
fprintf(fp,"%d,",result.first);
//https://blog.csdn.net/qq_41430551/article/details/90409644
fprintf(fp,"%lf,",result.second);
fprintf(fp,"%lf,",result.third);
fprintf(fp,"%lf\n",result.fourth);
i++;
if(i==NUM){
i=0;
}
}
//读取后面四列的数据
fscanf(tfp,"%lf",&message);
fscanf(tfp,"%d",&id);
fscanf(tfp,"%lf",&message);
fscanf(tfp,"%s\n",temp);
}
fclose(tfp);
fclose(fp);
return 0;
}
测试数据文件:
>CLSWCWEAWCUWCES WSF1 0 0.0 AWWMMMN 0 -0.231 01357900 5 9
< 0000000 0.000 0.000 0 0
< 10
< AAA 010 22.63 9178751.46 2123346.814 -98.310 233000 139724770280 62182e274
< AAA 011 22.61 9156751.50 2123345.861 -98.300 388000 139724770280 62182e274
< AAA 012 22.58 9124751.71 2123346.052 -98.306 388620 139724770280 62182e274
< AAA 013 22.63 9198751.42 2123350.619 -98.302 391000 139724770280 62182e274
< AAA 014 22.58 9138751.48 2123344.910 -98.302 391000 139724770280 62182e274
< AAA 015 22.68 9123351.49 2123345.861 -98.304 298000 139724770280 62182e274
< AAA 010 22.66 9178751.46 2123346.814 -98.310 233000 139724770280 62182e274
< AAA 011 22.52 9156751.50 2123345.861 -98.300 388000 139724770280 62182e274
< AAA 012 22.45 9124751.71 2123346.052 -98.306 388620 139724770280 62182e274
< AAA 013 22.78 9198751.42 2123350.619 -98.302 391000 139724770280 62182e274
>CLSWCWEAWCUWCES WSF1 0 0.0 AWWMMMN 0 -0.231 01357900 5 9
< 0000000 0.000 0.000 0 0
< 10
< AAA 010 22.52 9178751.52 2123346.814 -98.310 233000 139724770280 62182e274
< AAA 011 22.61 9156751.50 2123345.861 -98.300 388000 139724770280 62182e274
< AAA 012 22.58 9124751.71 2123346.052 -98.306 388620 139724770280 62182e274
< AAA 013 22.63 9198751.42 2123350.619 -98.302 391000 139724770280 62182e274
< AAA 014 22.58 9138751.48 2123344.910 -98.302 391000 139724770280 62182e274
< AAA 015 22.68 9123351.49 2123345.861 -98.304 298000 139724770280 62182e274
< AAA 010 22.66 9178751.46 2123346.814 -98.310 233000 139724770280 62182e274
< AAA 011 22.52 9156751.50 2123345.861 -98.300 388000 139724770280 62182e274
< AAA 012 22.45 9124751.71 2123346.052 -98.306 388620 139724770280 62182e274
< AAA 013 22.78 9198751.42 2123350.619 -98.302 391000 139724770280 62182e274
>CLSWCWEAWCUWCES WSF1 0 0.0 AWWMMMN 0 -0.231 01357900 5 9
< 0000000 0.000 0.000 0 0
< 10
< AAA 010 22.48 9178751.52 2123346.814 -98.310 233000 139724770280 62182e274
< AAA 011 22.61 9156751.50 2123345.861 -98.300 388000 139724770280 62182e274
< AAA 012 22.58 9124751.71 2123346.052 -98.306 388620 139724770280 62182e274
< AAA 013 22.63 9198751.42 2123350.619 -98.302 391000 139724770280 62182e274
< AAA 014 22.58 9138751.48 2123344.910 -98.302 391000 139724770280 62182e274
< AAA 015 22.68 9123351.49 2123345.861 -98.304 298000 139724770280 62182e274
< AAA 010 22.66 9178751.46 2123346.814 -98.310 233000 139724770280 62182e274
< AAA 011 22.52 9156751.50 2123345.861 -98.300 388000 139724770280 62182e274
< AAA 012 22.45 9124751.71 2123346.052 -98.306 388620 139724770280 62182e274
< AAA 013 22.78 9198751.42 2123350.619 -98.302 391000 139724770280 62182e274
运行程序后csv文件的结果: