每一步都打印文件

编程入门 行业动态 更新时间:2024-10-23 23:27:47
本文介绍了每一步都打印文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我有必须连续写入的数据,也就是说,这个数据是在循环中的每一步都更新了我需要在文件中打印这个。 所以,我需要的东西,如每一步,1.dat,2.dat ....等等。 如果有100个连续输出文件,为此,我想我需要100个 文件指针进行初始化。 FILE * fp_1 ,* fp_2等等.. .... .... .... for(i = 0; i< 100; i ++) { fp_no = fopen(" C:\\\\ opopno.dat"," w"); fprintf(fp_no,"%d,%lf",x [i],y [i]); fclose(fp_no); } 我想知道如何做到这一点......我一直在寻找 这个。有人可以就此提出建议吗? 提前致谢。 Mokkapati

解决方案

Mokkapati在05/18/06 17:37写道:

大家好, 我有必须连续编写的数据,也就是说,这个数据在循环的每一步都会更新,我需要在文件中打印这个数据。所以,我需要类似的东西,每一步,1.dat,2.dat ....等等。 如果有100个连续的输出文件,为此,我想我需要100 文件指针要初始化。 文件* fp_1,* fp_2等等.. ... ... ... for(i = 0; i <100; i ++) { fp_no = fopen(" C:\\\\ opopno.dat"," w"); fprintf (fp_no,"%d,%lf",x [i],y [i]); fclose(fp_no); } 我想知道的是怎么做以上...我一直在寻找这个。有人能建议我这个吗?

for(i = 0; i< 100; i ++){ char filename [sizeof " C:\\xxx.dat";; FILE * fp; sprintf(文件名,C:\\%d.dat ;,i); fp = fopen(filename," w"); if(fp == NULL)...处理错误... ... if(fclose(fp)!= 0)...处理错误... } - Er ********* @ sun

>我有必须连续写入的数据,也就是说,这个数据是

更新于循环中的每一步,我都需要在文件中打印它。所以,我需要的东西,如每一步,1.dat,2.dat ....等等。 fopen()的参数不必是常数。 sprintf()通常是 在构造字符数组中的文件名以传递给fopen()时很有用。 你不必使用它,但是,那里有多种方法可以构建 字符串。 从您描述的程序中,您不需要多于一个 文件同时打开。一个FILE *指针就足够了。 如果有100个连续的输出文件,为此,我想我需要100个文件指针进行初始化。 或者您可以重复使用相同的文件指针变量。 fopen(),给文件写一些,fclose(),重复。 FILE * fp_1,* fp_2等...... ... ... ... for(i = 0; i< 100; i ++) { fp_no = fopen(" C:\\\ \\ toopno.dat"," w"); fprintf(fp_no,"%d,%lf",x [i],y [i]); fclose(fp_no); }

Gordon L. Burditt

感谢Sosman先生的榜样和Burditt先生为了你的见解。我 可以运行示例代码。 非常感谢。 Mokkapati

Hi all, I have data that has to be written successively, that is, this data is updated at every step in the loop and I need to print this in a file. So, I need something like, for every step, 1.dat, 2.dat....and so on. If there are 100 successive output files, for that, I guess I need 100 file pointers to be initialized. FILE *fp_1,*fp_2 and so on.. .... .... .... for (i=0;i<100;i++) { fp_no = fopen("C:\\loopno.dat","w"); fprintf(fp_no,"%d, %lf", x[i], y[i]); fclose(fp_no); } I was wondering as to how to do the above...I have been looking for this since sometime. Can anyone suggest me something on this? Thanks in advance. Mokkapati

解决方案

Mokkapati wrote On 05/18/06 17:37,:

Hi all, I have data that has to be written successively, that is, this data is updated at every step in the loop and I need to print this in a file. So, I need something like, for every step, 1.dat, 2.dat....and so on. If there are 100 successive output files, for that, I guess I need 100 file pointers to be initialized. FILE *fp_1,*fp_2 and so on.. ... ... ... for (i=0;i<100;i++) { fp_no = fopen("C:\\loopno.dat","w"); fprintf(fp_no,"%d, %lf", x[i], y[i]); fclose(fp_no); } I was wondering as to how to do the above...I have been looking for this since sometime. Can anyone suggest me something on this?

for (i = 0; i < 100; i++) { char filename[sizeof "C:\\xxx.dat"]; FILE *fp; sprintf (filename, "C:\\%d.dat", i); fp = fopen(filename, "w"); if (fp == NULL) ... handle error ... ... if (fclose(fp) != 0) ... handle error ... } -- Er*********@sun

>I have data that has to be written successively, that is, this data is

updated at every step in the loop and I need to print this in a file.So, I need something like, for every step, 1.dat, 2.dat....and so on. The argument to fopen() need not be a constant. sprintf() is often useful in constructing a filename in a character array to pass to fopen(). You don''t have to use that, though, there are many ways of constructing strings from pieces. From the procedure you describe, you do not need more than one of these files open simultaneously. One FILE * pointer is enough. If there are 100 successive output files, for that, I guess I need 100file pointers to be initialized. Or you can use the same file pointer variable repeatedly. fopen(), write something to the file, fclose(), repeat. FILE *fp_1,*fp_2 and so on...........for (i=0;i<100;i++){ fp_no = fopen("C:\\loopno.dat","w"); fprintf(fp_no,"%d, %lf", x[i], y[i]); fclose(fp_no);}

Gordon L. Burditt

Thanks Mr. Sosman for the example and Mr. Burditt for your insights. I could run a sample code. Thanks so much. Mokkapati

更多推荐

每一步都打印文件

本文发布于:2023-11-26 21:18:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635228.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!