c语言程序,屏幕输出的所有结果保存到txt文档

#include
int main()
{
printf("Hello, World1! \n");
printf("Hello, World2! \n");
printf("Hello, World3! \n");
return 0;
}

该程序运行之后会在屏幕上输出
Hello, World1!
Hello, World2!
Hello, World3!
如何将这个输出的结果自动保存到一个txt的文档中?

5个回答

#include
#include
#include
int main()
{
FILE *fp=fopen("./hello.txt","w");
if(fp==NULL)
{
printf("创建文件失败\n");
return 0;
}
fprintf(fp,"Hello,World1!\n");
fprintf(fp,"Hello,World2!\n");
fprintf(fp,"Hello,World3!\n");
fclose(fp);
return 0;
}

FILE *fp;
if ((fp = fopen("D:\\hello.txt", "a")) == NULL)
{
    printf("can't open file");
    return  0;
}
fprintf(fp, "%s\n", "Hello, World1! \n");
fclose(fp);
 #include <stdio.h>

FILE *stream, *stream2;

void main( void )
{
   int numclosed;

   /* Open for read (will fail if file "data" does not exist) */
   if( (stream  = fopen( "data", "r" )) == NULL )
      printf( "The file 'data' was not opened\n" );
   else
      printf( "The file 'data' was opened\n" );

   /* Open for write */
   if( (stream2 = fopen( "data2", "w+" )) == NULL )
      printf( "The file 'data2' was not opened\n" );
   else
      printf( "The file 'data2' was opened\n" );

   /* Close stream */
   if( fclose( stream ) )
      printf( "The file 'data' was not closed\n" );

   /* All other files are closed: */
   numclosed = _fcloseall( );
   printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}

    StreamWriter sr = File.CreateText(文件名字);
    sr.WriteLine ("Hello, World1!");
    sr.WriteLine ("Hello, World12");
            sr.WriteLine ("Hello, World13");
    sr.Close();

生成可执行文件了么,如果有
./hello > log.txt

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐