用C写.CSV文件怎么写,急。。

用C写.CSV文件怎么写,急。。,第1张

#include"stdio.h"

#include"string.h"

#include<stdlib.h>

#include<conio.h>

struct Data{

char name[3]

char place[7]

char degree[7]

}

void main()

{

FILE *fr

int i

struct Data m

int count = 0

struct Data man[3] = { {"01","200801","2323.5"},{"02","200802","2656.9"},{"03","200803","5652.4"}

}

if((fr = fopen("test.csv","wt+")) == NULL)

{

printf("wrong")

exit(0)

}

for (i = 0i <3++i)

{

fprintf(fr,"%s,%s,%s\n",man[i].name,man[i].place,man[i].degree)////这样输出,用逗号隔开的

}

rewind(fr)

do

{

++count

fgets(m.name,3,fr)

fseek(fr,1L,1)

fgets(m.place,7,fr)

fseek(fr,1L,1)

fgets(m.degree,7,fr)

printf("%s\t%s\t%s\n",m.name,m.place,m.degree)

fseek(fr,2L,1)

}while(count <3)

fclose(fr)

getch()

}

对表面看到的结果是第四列变。

实质做法是:

1)把1、2、3列写入一个新文件,然后每行添加第4列

2)最后删除当前文件,把新文件名替换为当前文件。

当然如果数据量不大直接加入内存了,可以一次性覆盖本来文件写,不用改文件名这样的 *** 作。

连续输出多个逗号就行了

std::ofstream ofile("test.csv")

ofile << "R,G,B,H,S,V,R,G,B,H,S,V,回归方程,R,G,B,叶a,b,叶黄素" << std::endl // 第1行

ofile << ",,,,,,,R,G,B,H,S,V" << std::endl // 第2行


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11795562.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存