如何写入文件 [英] how to write in a file

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

问题描述

我如何在终端的plce文件中写一个ac prog的输出

使用重定向的方法

解决方案

4月18日06:06,sheilly_2k7< reply2shei ... @ gmail.comwrote:


如何编写ac prog的输出在终端的plce文件中

使用重定向无效



fopen(),fwrite(),fflush()。

http://c-faq.com/stdio/index。 html




" sheilly_2k7" < re *********** @ gmail.comwrote in message

news:11 ******************* ***@y80g2000hsf.googlegr oups.com ...


如何在终端的文件中写一个ac prog的输出

没有使用重定向



#include< stdio.h>


int main(void)

{

FILE * fp;


fp = fopen(" temp.txt"," w");

if(!fp)

fprintf(stderr,无法打开文件\ n);

其他

fprintf(fp,Hello file world \ n);

fclose(fp);

返回0;

}


未经测试但应该没问题。

-

免费游戏和编程好吃的东西。
http://www.personal.leeds.ac.uk/~bgy1mm


Malcolm McLean写道:

" sheilly_2k7" < re *********** @ gmail.comwrote in message


>我如何在文件中编写ac prog的输出在
终端的plce使用重定向



#include< stdio.h>


int main( void)

{

FILE * fp;


fp = fopen(" temp.txt"," w" );

if(!fp)

fprintf(stderr,不能打开文件\ n);

else

fprintf(fp,Hello file world \ n);

fclose(fp);

返回0;

}



这可能会关闭未打开的文件。相反,尝试:


#include< stdio.h>


int main(void){

FILE * fp;


if(!(fp = fopen(" temp.txt"," w")))

fprintf(stderr, 不能打开文件\ n;);

else {

fprintf(fp,Hello file world \ n);

fclose(fp);

}

返回0;

}


-

< http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

< http:// www.securityfocus.com/columnists/423>

< http://www.aaxnet.com/editor/edit043.html>


"每次都是正确的男人不太可能做得很多。

- 弗朗西斯克里克,共同发现DNA

没有什么比这更令人惊奇了在行动中愚蠢。

- 托马斯马修斯

-

通过 http://www.teranews.com


how can i write an output of a c prog in a file in plce of terminal
witout using redirection

解决方案

On 18 Apr, 06:06, sheilly_2k7 <reply2shei...@gmail.comwrote:

how can i write an output of a c prog in a file in plce of terminal
witout using redirection

fopen(), fwrite(), fflush().

http://c-faq.com/stdio/index.html



"sheilly_2k7" <re***********@gmail.comwrote in message
news:11**********************@y80g2000hsf.googlegr oups.com...

how can i write an output of a c prog in a file in plce of terminal
witout using redirection

#include <stdio.h>

int main(void)
{
FILE *fp;

fp = fopen("temp.txt", "w");
if(!fp)
fprintf(stderr, "Can''t open file\n");
else
fprintf(fp, "Hello file world\n");
fclose(fp);
return 0;
}

Untested but should be OK.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


Malcolm McLean wrote:

"sheilly_2k7" <re***********@gmail.comwrote in message

>how can i write an output of a c prog in a file in plce of
terminal witout using redirection


#include <stdio.h>

int main(void)
{
FILE *fp;

fp = fopen("temp.txt", "w");
if(!fp)
fprintf(stderr, "Can''t open file\n");
else
fprintf(fp, "Hello file world\n");
fclose(fp);
return 0;
}

That may close an unopened file. Instead, try:

#include <stdio.h>

int main(void) {
FILE *fp;

if (!(fp = fopen("temp.txt", "w")))
fprintf(stderr, "Can''t open file\n");
else {
fprintf(fp, "Hello file world\n");
fclose(fp);
}
return 0;
}

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
--
Posted via a free Usenet account from http://www.teranews.com


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

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