fprintf函数的运用(组图)

08-19栏目:技术

TAG:fprintf

fprintf

fprintf()用于文件操作

#include

int fprintf( FILE *stream, const char *format, ... ); copyright www.jhua.org

fprintf()函数根据指定的format(格式)发送信息(参数)到由stream(流)指定的文件.因此fprintf()可以使得信息输出到指定的文件.比如 jhua.org

char name[20] = "Mary";

FILE *out;

out = fopen( "output.txt", "w" );

if( out != NULL )

fprintf( out, "Hello %s\n", name );

copyright www.jhua.org

fprintf()和printf()一样工作.

https://www.jhua.org

printf是打印输出到屏幕,fprintf是打印输出到文件。 https://www.jhua.org

fprintf()的返回值是输出的字符数,发生错误时返回一个负值.

copyright www.jhua.org

在有些地方,有这样的定义:printf(…)=fprintf(stdout,…). copyright jhua.org

举例用法: copyright jhua.org

#include

#include

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" );

} jhua.org

屏幕输出:

copyright www.jhua.org

this is a string https://www.jhua.org

10 copyright www.jhua.org

1.500000

www.jhua.org

sprintf

sprintf 是个变参函数,定义如下: copyright www.jhua.org

int sprintf( char *buffer, const char *format [, argument] ... ); copyright jhua.org

函数功能:把格式化的数据写入某个字符串 copyright jhua.org

函数原型:int sprintf( char *buffer, const char *format [, argument] … );

https://www.jhua.org

返回值:字符串长度(strlen)

copyright jhua.org

例子: copyright www.jhua.org

char* who = "I";

char* whom = "CSDN";

sprintf(s, "%s love %s.", who, whom); //产生:"I love CSDN. " 这字符串写到s中

sprintf(s, "%10.3f", 3.1415626); //产生:" 3.142"

copyright jhua.org

char a1[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};

char a2[] = {'H', 'I', 'J', 'K', 'L', 'M', 'N'};

如果:

sprintf(s, "%s%s", a1, a2); //Don't do that!

//十有八九要出问题了。是否可以改成:

sprintf(s, "%7s%7s", a1, a2);

//也没好到哪儿去,正确的应该是:

sprintf(s, "%.7s%.7s", a1, a2);//产生:"ABCDEFGHIJKLMN"

www.jhua.org

这可以类比打印浮点数的”%m.nf”,在”%m.ns”中,m 表示占用宽度(字符串长度不足时补空 jhua.org

格,超出了则按照实际宽度打印),n 才表示从相应的字符串中最多取用的字符数。通常在打印字 copyright www.jhua.org

符串时m 没什么大用,还是点号后面的n 用的多。自然,也可以前后都只取部分字符:

https://www.jhua.org

sprintf(s, "%-*d", 4, 'A'); //产生"65 "

sprintf(s, "%#0*X", 8, 128); //产生"0X000080","#"产生0X

sprintf(s, "%*.*f", 10, 2, 3.1415926); //产生" 3.14" copyright jhua.org

1,printf就是标准输出,在屏幕上打印出一段字符串来。 jhua.org

2,sprintf就是把格式化的数据写入到某个字符串中。返回值字符串的长度。

www.jhua.org

3,fprintf是用于文件操作。 www.jhua.org

fprintf()中的 stderr说明

先看一个小例子: copyright www.jhua.org

#include

void main()

{

fprintf(stderr,"can't open it!");

fprintf(stdout,"can't open it !");

printf("can't open it!");

} copyright www.jhua.org

上面程序编译成fprint文件,运行显示如下: jhua.org

Can't open it! Can't open it! Can't open it! copyright www.jhua.org

若将输入重定向到一个temp.txt文件中,运行:./fprint >temp.txt 结果如下: copyright jhua.org

Can’t open it! copyright www.jhua.org

查看temp.txt文件内容为:

copyright jhua.org

Can’t open it!Can’t open it! copyright www.jhua.org

说明: copyright jhua.org

stdout – 标准输出设备 (printf(“..”)) 同 stdout。 copyright www.jhua.org

stderr – 标准错误输出设备

copyright www.jhua.org

两者默认向屏幕输出。

jhua.org

但如果用转向标准输出到磁盘文件,则可看出两者区别。stdout输出到磁盘文件,stderr在屏幕。 copyright jhua.org

strerr是作为程序运行过程中的错误显示出来的,若想将它重写向到某文件中,需要运行如下命令:

copyright www.jhua.org

./fprint 2>temp.txt jhua.org

这样运行结果就为: copyright www.jhua.org

Can’t open it!Can’t open it! https://www.jhua.org

查看temp.txt文件的内容是: copyright www.jhua.org

Can’t open it! copyright jhua.org

stdin

stdin – 标准输入设备.

copyright jhua.org

用法: copyright jhua.org

char s[80];

copyright www.jhua.org

fputs(fgets(s,80,stdin);

www.jhua.org

问题:下面程序的输出是什么?(intel笔试2011) copyright www.jhua.org

int main(){

fprintf(stdout,"Hello ");

fprintf(stderr,"World!");

return0;

}

https://www.jhua.org

解答:这段代码的输出是什么呢?你可以快速的将代码敲入你电脑上(当然,拷贝更快),然后发现输出是 jhua.org

World!Hello

jhua.org

这是为什么呢?在默认情况下,stdout是行缓冲的,他的输出会放在一个buffer里面,只有到换行的时候,才会输出到屏幕。而stderr是无缓冲的,会直接输出,举例来说就是printf(stdout, “xxxx”) 和 printf(stdout, “xxxx\n”),前者会憋住,直到遇到新行才会一起输出。而printf(stderr, “xxxxx”),不管有么有\n,都输出。 copyright jhua.org

2, www.jhua.org

fprintf(stderr, "Can't open it!\n");

fprintf(stdout, "Can't open it!\n");

printf("Can't open it!\n");

https://www.jhua.org

这3句效果不是一样啊,有什么区别吗? jhua.org

有区别。 copyright www.jhua.org

stdout – 标准输出设备 (printf(“..”)) 同 stdout。 www.jhua.org

stderr – 标准错误输出设备

https://www.jhua.org

两者默认向屏幕输出。

https://www.jhua.org

但如果用转向标准输出到磁盘文件,则可看出两者区别。stdout输出到磁盘文件,stderr在屏幕。

www.jhua.org

例如: https://www.jhua.org

my.exe

www.jhua.org

Can’t open it!

https://www.jhua.org

Can’t open it!

copyright jhua.org

Can’t open it!

copyright jhua.org

转向标准输出到磁盘文件tmp.txt

https://www.jhua.org

my.exe > tmp.txt

copyright www.jhua.org

Can’t open it! www.jhua.org

用TYPE 看 tmp.txt的内容: www.jhua.org

TYPE tmp.txt jhua.org

Can’t open it!

jhua.org

Can’t open it!

copyright www.jhua.org

相关阅读

Matlab中自定义函数(一) jhua.org

作为一个程序员出生的Matlab学习者,不能定义函数那简直是受不了!!

最重要的一点!

定义函数的时候,很多时候都会很迷的一般,使用不了 www.jhua.org

wait(),waitpid()函数

jhua.org

首先我们来了解一下所谓的僵尸进程,僵尸进程就是两个进程,一个父进程,一个子进程,其子进程终止后,0-3G的用户内存被回收,而3-4G的部分内 https://www.jhua.org

C/C++ 学习笔记:istringstream、ostringstream、string https://www.jhua.org

0、C++的输入输出分为三种:(1)基于控制台的I/O(2)基于文件的I/O(3)基于字符串的I/O 1、头文件[cpp] view plaincopyprint?

#incl

jhua.org

container of()函数简介 jhua.org

在linux 内核编程中,会经常见到一个宏函数container_of(ptr,type,member), 但是当你通过追踪源码时,像我们这样的一般人就会绝望了(

https://www.jhua.org

matlab中contour 函数的用法(绘制等高线)

copyright www.jhua.org

原文contour矩阵的等高线图全页折叠语法contour(Z)contour(Z,n)contour(Z,v)contour(X,Y,Z)contour(X,Y,Z,n)contour(X,Y,Z,v)con www.jhua.org

阅读量:100000+

上一篇:什么浏览器好用稳

推荐量:8761

下一篇:JMH初探[多图]

fprintf函数matlab,fprintf函数的运用(组图)相关推荐

  1. matlab 定义函数 调用,matlab 定义函数,matlab定义函数并调用

    matlab 定义函数,matlab定义函数并调用,Matlab自定义函数详解 很久以前写的一篇Matlab自定义函数访问量很大,可惜没有点赞的,我感觉是我没讲清楚,这里又写了一篇笔记 Matlab函 ...

  2. 如何用matlab编写分段函数_请教各位怎样用matlab定义一个分段函数MATLAB分段函数...

    请教各位怎样用matlab定义一个分段函数 MATLAB分段函数 www.zhiqu.org     时间: 2020-12-08 matlab提供了了两种定义分段函数的方法: 常规方法:使用if.. ...

  3. matlab 设置为匿名函数,MATLAB匿名函数

    本文概述 匿名函数是简单的(单行)用户定义函数, 无需创建单独的函数文件(M文件)即可进行定义.可以在命令窗口中, 脚本文件中或用户定义的函数中定义匿名函数. 通过键入以下命令来生成匿名函数: 其中f ...

  4. matlab normc函数,matlab normc函数

    用Matlab命令:normc(X)或norm(X) ? 均值中心化:从每个变量... 用Matlab进行系统函数H(s)仿真 Matlab进行系统函数 进行系统函数H(s)仿真---无01班---无 ...

  5. matlab doc函数,matlab常用函数.doc

    matlab常用函数.doc MatLab 常用函数 1. 特殊变量与常数 ans 计算结果的变量名 computer 确定运行的计算机 eps 浮点相对精度 Inf 无穷大 I 虚数单位 name ...

  6. matlab私有函数,MATLAB 嵌套函数,子函数,私有函数,重载函数

    MATLAB函数嵌套 MATLAB中M文件有两种类型,脚本M文件和函数M文件.脚本M文件是将可执行程序语句放入M文件中,就像在命令窗口那样,按其语句顺序及逻辑关系执行,可以理解为一般的顺序执行程序语句 ...

  7. matlab swt函数,matlab swt 函数出错

    matlab swt 函数出错 我在用matlab swt 函数分解信号时总是出现以下错误,麻烦各位高手告知该怎么修改,swt函数如何ERROR ... ----------------------- ...

  8. 怎么调出matlab的函数,matlab定义函数【搞定方法】

    喜欢使用电脑的小伙伴们一般都会遇到win7系统matlab定义函数的问题,突然遇到win7系统matlab定义函数的问题就不知道该怎么办了,其实win7系统matlab定义函数的解决方法非常简单,按照 ...

  9. matlab if嵌套函数,MATLAB嵌套函数的应用

    嵌套函数在求解积分上限中的应用 例1如下述积分表达式,已知a.e和l,如何求得β0? 本例关于β的积分结果不能解析表达,需要数值积分来做,同时还要求一个非线性方程.代码如下: function sol ...

  10. c语言不用死等的延时函数,matlab延时函数怎么写

    1. c语言延时函数delay,怎么算延时 下面是delay 函延迟函数里执行的都是空语句,也就是说通过循环执行空语句来达到延迟的目的.每执行一条语句,即使是空语句都要耗费电脑一些处理时间的,就是因为 ...

最新文章

  1. HDU2032(杨辉三角)
  2. [转]面向GPU的多LOD因子的大规模场景可视化策略
  3. 【Docker实战之入门】Dockerfile详细分析:构建docker镜像(4)构建动态网站WordPress...
  4. 结队作业,小学生3年级数学题出题器
  5. 配置MGR时修改了/etc/hosts但映射后的hostname不起作用
  6. linux4.14内核,Linux内核4.14.14,4.9.77,4.4.112和3.18.92更新发布
  7. Django中级篇之模板语言
  8. 展望2021,Java、Go、.NET,谁主沉浮?
  9. 删除rz上传失败乱码的文件
  10. java mvc页面传值方式_详解SpringMVC的ModelAndView传值方法
  11. Quartz框架调用Demo
  12. Python 之pdb调试
  13. 管理感悟:知行合一与内化
  14. 新手linux版本,六款适用于新手的非Ubuntu Linux发行版
  15. matlab函数表达式里分号_matlab中分号、冒号、逗号等常用标点符号的功能和用法总结...
  16. Hacking Tools简介
  17. 嗨购,共享购商业模式,让你的店铺盈利更轻松
  18. 深圳区块链企业在江岸区设区域总部
  19. Apple 投诉网站
  20. flarum 微信登录修改

热门文章

  1. jieba分词及词性判断
  2. 计算机电子表格减法公式,excel表格公式怎么操作
  3. 一、 Vue.js简介
  4. 关于周考的总结与反思
  5. Less + HTML + JS实现流星划过星空动画
  6. Typora配置PicGo提示Failed to fetch问题解决
  7. 【人工智能】一文读懂人脸识别技术
  8. 使用Sklearn学习决策树
  9. python爬虫实战(七) 爬取B站柯南弹幕+Gephi绘制人物画像
  10. android+微博点赞动画,模仿微博点赞动画