操作系统大型实验进展(10)-----fprintf
2013-01-09 22:29
260 查看
fprintf
是C/C++中的一个格式化写—库函数;其作用是格式化输出到一个流/文件中;函数完整形式: int fprintf(FILE *stream,char *format,[argument])
功 能
传送格式化输出到一个文件中,可用于打印机输出。
用 法
#include <stdio.h>int fprintf( FILE *stream, const char *format, ... );
fprintf()函数根据指定的format(格式)发送信息(参数)到由stream(流)指定的文件. fprintf()只能和printf()一样工作. fprintf()的返回值是输出的字符数,发生错误时返回一个负值.
返回值
若成功则返回输出字符数,若输出出错则返回负值。程序例
/* Program to create backup of theAUTOEXEC.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.BAT", "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;
int 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
例二
#include <stdio.h>
int main()
{
FILE *fp;
int i=617;
char* s = "that is a good new";
fp = fopen("text.dat","w");
fputs("total",fp);
fputs(":",fp);
fprintf(fp,"%d\n",i);
fprintf(fp,"%s",s);
fclose(fp);
return 0;
}
输出
total:617
that is a good new
格式化规定符
%d 十进制有符号整数
%u 十进制无符号整数
%f 浮点数
%s 字符串
%c 单个字符
%p 指针的值
%e 指数形式的浮点数
%x, %X 无符号以十六进制表示的整数
%0 无符号以八进制表示的整数
%g 自动选择合适的表示法
转自百度百科
相关文章推荐
- 操作系统大型实验进展(7)-----C语言函数fopen
- 操作系统大型实验进展(8)-----C语言函数fread
- 操作系统大型实验进展(13)-----bieset
- 操作系统大型实验进展(15)-----printf进行格式化输出
- 操作系统大型实验进展(1)-----c++ 初始化文件
- 操作系统大型实验进展(12)----- fwrite 0D 0A 问题
- 操作系统大型实验进展(2)-----头文件保护
- 操作系统大型实验进展(9)-----strlen()和sizeof
- 操作系统大型实验进展(3)-----C++常见数据类型字节数
- 操作系统大型实验进展(4)-----C/C++如何获取系统时间
- 操作系统大型实验进展(11)-----C/C++语言在头文件中定义全局变量
- 操作系统大型实验进展(5)-----C/C++语言中time_t数据类型详细介绍
- 操作系统大型实验进展(6)-----Linux 内核文件系统与设备操作流程分析
- 操作系统大型实验进展(14)-----strtok函数在C和C++语言中的使用
- CCNP路由实验---10、OSPF非绝对末节区域的配置
- (操作系统)实验二 作业调度
- 操作系统实验四:保护模式之局部任务(LDT)初探
- 操作系统实验报告 lab3
- 关于大型网站技术演进的思考(十八)--网站静态化处理―反向代理(10)
- 实验报告 10 --1