求救!!如何在c语言中用fprintf函数输出txt文件

如题所述

fprintf 简介   c/c++语言函数: fprintf 功 能   传送格式化输出到一个文件中 用 法   #include <stdio.h>   int fprintf( FILE *stream, const char *format, ... );   fprintf()函数根据指定的format(格式)(格式)发送信息(参数)到由stream(流)指定的文件. fprintf()只能和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值. 返回值   成功时返回转换的字节数,失败时返回一个负数.   在LINUX/UNIX操系统中成功返回0,失败返回-1。并置errno值. 程序例   /* Program to create backup of the   AUTOEXEC.BAT file */   #include <stdio.h>   int main(void)   {   FILE *in, *out;   if ((in = fopen("//AUTOEXEC.BAT", "rt")) == NULL)   {   fprintf(stderr, "Cannot open input file./n");   return 1;   }   if ((out = fopen("//AUTOEXEC.BAK", "wt")) == NULL)   {   fprintf(stderr, "Cannot open output file./n");   return 1;   }   while (!feof(in))   fputc(fgetc(in), out);   fclose(in);   fclose(out);   return 0;   }   举例用法:   #include <stdio.h>   #include <process.h>   FILE *stream;   void main( void )   {   int i = 10;   double fp = 1.5;   char s[] = "this is a string";   char c = '/n';   stream = fopen( "fprintf.out", "w" );   fprintf( stream, "%s%c", s, c );   fprintf( stream, "%d/n", i );   fprintf( stream, "%f/n", fp );   fclose( stream );   system( "type fprintf.out" );   }   屏幕输出:   this is a string   10   1.500000   格式化规定符   %d 十进制有符号整数   %u 十进制无符号整数   %f 浮点数   %s 字符串   %c 单个字符   %p 指针的值   %e 指数形式的浮点数   %x, %X 无符号以十六进制表示的整数   %0 无符号以八进制表示的整数   %g 自动选择合适的表示法
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-06
先用open函数打开一个txt文件,格式:fp=fopen(" ***.txt ","w");
再用fprintf(fp,“******");输入到文本。
或者fprintf(fp,"%d,%d",i,j);
第2个回答  推荐于2016-03-10
简单的来说:

FILE *fp = fopen(“*.txt”, "w");
fprintf(fp, "这里放置你需要输出到txt文件的字符流!",...);
fclose(fp)本回答被网友采纳
相似回答