每一步都打印文件 [英] Printing files every step

查看: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); < br $>
}


我想知道如何做到这一点......我一直在寻找

这个。有人可以就此提出建议吗?


提前致谢。


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.com


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

更新于循环中的每一步,我都需要在文件中打印它。
所以,我需要的东西,如每一步,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.com


>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 100
file 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


这篇关于每一步都打印文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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