当前位置: 首页 > ai >正文

细说C语言将格式化输出到FILE *stream流的函数fprintf、_fprintf_I、fwprintf、_fwprintf_I

目录

1、将格式化数据输出到FILE *stream流基本型

(1)语法

(2)参数

(3)示例

(4)运行结果

2、将格式化数据输出到FILE *stream流并启用并启用在格式字符串中使用参数的顺序的规范

(1)语法

(2)参数

(3)示例

(4)运行结果

3、将格式化数据输出到FILE *stream流安全版本

(1)语法

(2)参数

(3)示例

(4)运行结果


        C语言将格式化输出到FILE *stream流的函数的外观表达是以fprintf()为根的。将格式化输出到FILE *stream流的函数有:

1、将格式化数据输出到FILE *stream流基本型

        fprintf、_fprintf_I、fwprintf、_fwprintf_I。

(1)语法

/**
fprintf 格式化一系列字符和值并将其输出到输出 stream。 每个 argument 函数(如果有)根据 format 中相应的格式规范进行转换和输出。 对于 fprintf,format 参数具有与 printf 中相同的语法。
fwprintf 是 fprintf 的宽字符版本;在 fwprintf 中,format 是宽字符字符串。 如果在 ANSI 模式下打开流,则这些函数行为相同。 fprintf 当前不支持到 UNICODE 流中的输出。
这些带有 _l 后缀的函数的版本相同,只不过它们使用传递的区域设置参数而不是当前线程区域设置。
*/int fprintf(FILE *stream,const char *format [,argument ]...
);
int _fprintf_l(FILE *stream,const char *format,_locale_t locale [,argument ]...
);
int fwprintf(FILE *stream,const wchar_t *format [,argument ]...
);
int _fwprintf_l(FILE *stream,const wchar_t *format,_locale_t locale [,argument ]...
);

(2)参数

  • stream:指向 FILE 结构的指针。
  • format:窗体控件字符串。
  • argument:可选参数。
  • locale:要使用的区域设置。
  • 返回值:fprintf 返回已写入的字节数。 fwprintf 返回已写入的宽字符数。其中每个函数在出现输出错误时返回一个负值。

(3)示例

// test_fprintf.c
/* This program uses fprintf to format various
* data and print it to the file named FPRINTF.OUT. It
* then displays FPRINTF.OUT on the screen using the system
* function to invoke the operating-system TYPE command.
*/#include <stdio.h>
#include <process.h>FILE* stream;
errno_t err;int main(void)
{int i = 10;double fp = 1.5;char s[] = "this is a string";char c = '\n';//err的作用:在调用 fclose 函数前添加空指针检查,可以避免警告 C6387err = fopen_s(&stream, "fprintf.out", "w");if (err != 0) puts("文件打开失败\n");else {printf("文件打开成功\n");				//可以注释掉if (stream != NULL){fprintf(stream, "%s%c", s, c);fprintf(stream, "%d\n", i);fprintf(stream, "%f\n", fp);fclose(stream);}}system("type fprintf.out");
}

(4)运行结果

文件打开成功
this is a string
10
1.500000C:\Users\YCZN_MT\Test_fprintf\x64\Debug\Test_fprintf.exe (进程 18692)已退出,代码为 0 (0x0)。
按任意键关闭此窗口. . .

2、将格式化数据输出到FILE *stream流并启用并启用在格式字符串中使用参数的顺序的规范

         _fprintf_p、_fprintf_p_l、_fwprintf_p、_fwprintf_p_I。

(1)语法

/**
_fprintf_p 格式化一系列字符和值并将其输出到输出 stream。 每个 argument 函数(如果有)根据 format 中相应的格式规范进行转换和输出。 对于 _fprintf_p,format 参数具有与 _printf_p 中相同的语法。 这些函数支持位置参数,即可以更改格式字符串所使用的参数顺序。 
_fwprintf_p 是 _fprintf_p 的宽字符版本;在 _fwprintf_p 中,format 是宽字符字符串。 如果在 ANSI 模式下打开流,则这些函数行为相同。 _fprintf_p 当前不支持到 UNICODE 流中的输出。
这些带有 _l 后缀的函数的版本相同,只不过它们使用传递的区域设置参数而不是当前区域设置。
*/int _fprintf_p(FILE *stream,const char *format [,argument ]...
);
int _fprintf_p_l(FILE *stream,const char *format,_locale_t locale [,argument ]...
);
int _fwprintf_p(FILE *stream,const wchar_t *format [,argument ]...
);
int _fwprintf_p_l(FILE *stream,const wchar_t *format,_locale_t locale [,argument ]...
);

(2)参数

  • stream:指向 FILE 结构的指针。
  • format:窗体控件字符串。
  • argument:可选参数。
  • locale:要使用的区域设置。
  • 返回值:如果发生输出错误,则 _fprintf_p 和 _fwprintf_p 返回写入的字符数或一个负值。
  • _p:启用在格式字符串中使用参数的顺序的规范。

(3)示例

// test_fprintf_p.c
// This program uses _fprintf_p to format various
// data and print it to the file named FPRINTF_P.OUT. It
// then displays FPRINTF_P.OUT on the screen using the system
// function to invoke the operating-system TYPE command.
//#include <stdio.h>
#include <process.h>int main(void)
{FILE* stream = NULL;int     i = 10;double  fp = 1.5;char    s[] = "this is a string";char    c = '\n';// Open the fileif (fopen_s(&stream, "fprintf_p.out", "w") == 0){// Format and print data_fprintf_p(stream, "%2$s%1$c", c, s);    //在格式字符串中使用了参数顺序_fprintf_p(stream, "%d\n", i);               //此处应用等价于fprintf_fprintf_p(stream, "%f\n", fp);_fprintf_p(stream, "%2$s%1$c%3$d\n%4$f\n", c, s,i,fp);    //在格式字符串中使用了参数顺序_fprintf_p(stream, "%3$d\n%4$f\n%2$s%1$c", c, s,i,fp);    //在格式字符串中使用了参数顺序// Close the filefclose(stream);}// Verify our datasystem("type fprintf_p.out");
}

(4)运行结果

this is a string
10
1.500000
this is a string
10
1.500000
10
1.500000
this is a stringC:\Users\YCZN_MT\Test_fprinf_p\x64\Debug\Test_fprinf_p.exe (进程 21844)已退出,代码为 0 (0x0)。
按任意键关闭此窗口. . .

3、将格式化数据输出到FILE *stream流安全版本

         fprintf_s、_fprintf_s_l、fwprintf_s、fwprintf_s_l

(1)语法

/**
fprintf_s 格式化一系列字符和值并将其输出到输出 stream。 argument_list 中的每个参数(如果有)根据 format 中相应的格式规范进行转换和输出。 format 参数使用 printf 和 wprintf 函数的格式规范语法。
fwprintf_s 是 fprintf_s 的宽字符版本;在 fwprintf_s 中,format 是宽字符字符串。 如果在 ANSI 模式下打开流,则这些函数行为相同。 fprintf_s 当前不支持到 UNICODE 流中的输出。
这些带有 _l 后缀的函数的版本相同,只不过它们使用传递的区域设置参数而不是当前区域设置。
*/int fprintf_s(FILE *stream,const char *format [,argument_list ]
);
int _fprintf_s_l(FILE *stream,const char *format,_locale_t locale [,argument_list ]
);
int fwprintf_s(FILE *stream,const wchar_t *format [,argument_list ]
);
int _fwprintf_s_l(FILE *stream,const wchar_t *format,_locale_t locale [,argument_list ]
);

(2)参数

  • stream:指向 FILE 结构的指针。
  • format:窗体控件字符串。
  • argument_list:格式字符串的可选参数。
  • locale:要使用的区域设置。
  • 返回值:fprintf_s 返回已写入的字节数。 fwprintf_s 返回已写入的宽字符数。 其中每个函数在出现输出错误时返回一个负值。

(3)示例

// test_fprintf_s.c
// This program uses fprintf_s to format various
// data and print it to the file named FPRINTF_S.OUT. It
// then displays FPRINTF_S.OUT on the screen using the system
// function to invoke the operating-system TYPE command.#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';errno_t err;//err的作用:在调用 fclose 函数前添加空指针检查,可以避免警告 C6387err = fopen_s(&stream, "fprintf_s.out", "w");if (err != 0)puts("文件打开失败\n");else {//printf("文件打开成功\n");				//可以注释掉if (stream != NULL){fprintf_s(stream, "%s%c", s, c);fprintf_s(stream, "%d\n", i);fprintf_s(stream, "%f\n", fp);fclose(stream);}}system("type fprintf_s.out");
}

(4)运行结果

this is a string
10
1.500000C:\Users\YCZN_MT\Test_fprintf_s\x64\Debug\Test_fprintf_s.exe (进程 21396)已退出,代码为 0 (0x0)。
按任意键关闭此窗口. . .

http://www.xdnf.cn/news/11868.html

相关文章:

  • 轴承排列自动运行 定时器 外中断 PWM部分程序
  • 使用 systemctl 实现程序自启动与自动重启
  • RAG技术解析:实现高精度大语言模型知识增强
  • 【运维实战】Rsync将一台主Web服务器上的文件和目录同步到另一台备份服务器!
  • 数据库基础篇
  • 文件解读|检索页(附:新版知网国内刊检索页下载方法!)
  • cv::FileStorage用法
  • 多线程爬虫使用代理IP指南
  • Java面试题及答案整理( 2025年最新版,持续更新...)
  • PARADISE:用于新生儿缺氧缺血性脑病(HIE)疾病识别与分割的个性化和区域适应性方法|文献速递-深度学习医疗AI最新文献
  • GMS地下水数值模拟及溶质(包含反应性溶质)运移模拟技术
  • Python爬虫之数据提取
  • JavaScript性能优化实战技术
  • LeetCode-934. 最短的桥
  • 【uniapp开发】picker组件的使用
  • 二叉数-965.单值二叉数-力扣(LeetCode)
  • JavaWeb:前端工程化-Vue
  • 舵机在弹簧刀无人机中的作用是什么?
  • Linux 进程调度与管理:从内核管理到调度机制的深度解析
  • 【前端AI实践】泛谈AI在前端领域的应用场景
  • Vue-Todo-list 案例
  • 【QT】-信号传输数组跨线程段错误处理
  • Go语言依赖管理与版本控制-《Go语言实战指南》
  • 【使用 Loki + Promtail + Grafana 搭建轻量级容器日志分析平台】
  • 【Linux系统】命令行参数 和 环境变量(含内建命令介绍)
  • NLP常用工具包
  • video-audio-extractor【源码版】
  • 出口合规管理
  • 在 Android Studio 中使用 GitLab 添加图片到 README.md
  • 【免费数据】1980-2022年中国2384个站点的水质数据