如何使用fprintf中用于将数据写入文件 [英] How to use fprintf for writing data to a file

查看:1178
本文介绍了如何使用fprintf中用于将数据写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C程序数据写入一个文件,以便Excel可以读取该文件绘制数据图。但我不知道确切的语法使用fprintf中的。
我在我的程序的顶部已经stdlib.h中声明。我宣布文件*计划生育;在主要的,但我发现的文件和FP是未申报。可能是什么问题?

**编辑:我的程序编译和运行,但现在我的输出文件不包含任何数据
这是我在while循环,做一些计算。

的结束

 计划生育=的fopen(out_file.txt,W); //写入打开文件 fprintf中(FP,X =%F,Y =%F,VX =%F,VY =%F,时间=%F,X,Y,VX,VY,时间);


解决方案

您逻辑应该是这个样子:

 计划生育=的fopen(out_file.txt,W); //写入打开文件而(一些条件)
{    ...一些计算    fprintf中(FP,X =%F,Y =%F,VX =%F,VY =%F,时间=%F,X,Y,VX,VY,时间);
}FCLOSE(FP);

I want to write data from a C program to a file, so that Excel can read the file to plot a graph of the data. But I'm not sure of the exact syntax to use for fprintf. I have stdlib.h declared in the very top of my program. I declared "File *fp;" in main but I'm getting that File and fp are undeclared. What could be the problem?

**EDIT: My program compiles and runs but now my output file doesn't contain any data This is what I have at the end of a while loop that does some computations..

 fp = fopen( "out_file.txt", "w" ); // Open file for writing

 fprintf(fp, "x = %f, y = %f, vx = %f, vy = %f, time = %f, ", x,y,vx,vy,time);

解决方案

Your logic should look something like this:

fp = fopen( "out_file.txt", "w" ); // Open file for writing

while ( some condition )
{

    ... some calculations

    fprintf(fp, "x = %f, y = %f, vx = %f, vy = %f, time = %f, ", x,y,vx,vy,time);
}

fclose(fp);

这篇关于如何使用fprintf中用于将数据写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆