C语言如何将一个double类型的数组写入文件?

cysolo 2012-10-09 11:37:23
假设该数组为 double buff[1000],写入result.txt中。
...全文
2389 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mingky 2012-10-11
  • 打赏
  • 举报
回复
FILE *fp_out;
double buff[1000];
fp_out=fopen("result.txt","w");
for(i=0;i<1000;i++) {fprintf(fp_out, "%f%c", buff[i],',');}
whoozit 2012-10-10
  • 打赏
  • 举报
回复
你这问题实在太入门,不过我现在也实在闲的很。


void SaveBuff(FILE *p, double *d, int n)
{
int i;

for(i=0; i<n; i++)
{
fprintf(p, "%lf\n", d[i]);
}
}


用法:

SaveBuff(fp, buff, sizeof(buff)/sizeof(double));


例子:

#include <stdio.h>
#include <assert.h>
#include <math.h>

void SaveBuff(FILE *, double *, int);

int main()
{
int i;
double buff[1000];
FILE *fp = fopen("result.txt", "w");

assert(fp);

for(i=0; i<1000; i++)
buff[i] = sin((double)i);

SaveBuff(fp, buff, sizeof(buff)/sizeof(double));

fclose(fp);
return 0;
}

void SaveBuff(FILE *p, double *d, int n)
{
int i;

for(i=0; i<n; i++)
{
fprintf(p, "%lf\n", d[i]);
}
}
lin5161678 2012-10-10
  • 打赏
  • 举报
回复
fprintf
fwrite
随你喜欢
AnYidan 2012-10-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

如果要文本可读,使用fprintf写,格式化写入,用法参考printf
[/Quote]

txt 肯定可读
jackyjkchen 2012-10-09
  • 打赏
  • 举报
回复
如果要文本可读,使用fprintf写,格式化写入,用法参考printf

70,014

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧