其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
在C中如何把print出的文字,紀錄在一個文件中

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

方法一:
#include<stdio.h>
main()
{
  FILE *fp;
  int i=0;
  char *s="Am I right?";
  fp=fopen("c:\\text.txt","wr");
  while(*s)
  { printf("%c",*s);
    fseek(fp,i++,SEEK_SET);
    fprintf(fp,"%c",*s++);
  }
  fclose(fp);
  getchar();
}


方法二:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
main()
{
  FILE  *fp;
  char  ch;
  fp = fopen("F:\\file.txt","wb");   //写的方式打开文件,你把file.txt换成你自己的文件就ok
  if( fp == NULL)
  {  printf("can't open file.txt\n");  exit(1); }  //打开文件失败则退出
  scanf("%c",&ch);
  while(ch != '*')          //输入字符*则退出
  {
    printf("%c\n",ch);
    fputc(ch,fp);           //将ch写入你打开的文件
    fflush(stdin);          //清除输入缓冲区
    scanf("%c",&ch);        //继续输入一个字符
  }
  printf("input over!\n");
  getch();
}

方法三:

#include <stdio.h>
#include <stdarg.h>

void xprintf(const char *pszFormat, ...)
{
 char buf[1024] = {0};
 FILE *cfPtr = fopen("1.log", "a");
 va_list arg_ptr;
 va_start(arg_ptr, pszFormat);
 vsprintf(buf, pszFormat, arg_ptr);
 va_end(arg_ptr);
 printf("%s", buf);
 if (cfPtr!=NULL)
 {
  fprintf(cfPtr, "%s", buf);
  fclose(cfPtr);
 }
}

int main(int argc, char *argv[])
{
 xprintf("%-10d%s\n", 99, "hello world");
 return 0;
}


方法四:


void WriteDat(void)
{
 int i;

 char xx[2][23]={"You He Me","I am a student."};//测试数据.
 FILE *fp;

 fp=fopen("OUT6.txt","a+");
 for(i=0;i<2;i++)
 {
  printf("%s\n",xx[i]);//运行时在屏幕上显示
  fprintf(fp,"%s\n",xx[i]);//写入文件,
 }
 fclose(fp);
}

方法五:

手动可以用重定向
a.out > out.txt
程序实现可以用fprintf
#include<stdio.h>
main()
{
  FILE *fp;
  int i=0;
  char *s="Am I right?";
  fp=fopen("c:\\text.txt","wr");
  while(*s)
  { printf("%c",*s);
    fseek(fp,i++,SEEK_SET);
    fprintf(fp,"%c",*s);
    s++;
  }
  fclose(fp);
  getchar();
}




相关文章

相关软件