目录
- 一.fprintf 函数简介
- 二.fprintf 函数使用
- 三.猜你喜欢
一.fprintf 函数简介零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门
fprintf 是 C / C++ 中的一个格式化库函数,位于头文件 中,其作用是格式化输出到一个流文件中;函数原型为
int fprintf (FILE* stream, const char*format, [argument]);
fprintf 函数是变参函数,format 可以由一个或者多个参数构成,案例如下:
//示例: fprintf(stream,"www.codersrc.comn"); fprintf(stream,"www.codersrc.com age:%dn",17); fprintf(stream,"www.codersrc.com age:%d name:%sn",17, "zhangsan"); fprintf(stream,"www.codersrc.com age:%d name:%s height:%fn",17, "zhangsan",1.75);二.fprintf 函数使用
//@Author:猿说编程 //@Blog(个人博客地址): www.codersrc.com //@File:C语言教程 - C语言 fprintf 函数 //@Time:2021/07/30 07:30 //@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! #include#include #include int main() { //Initialize the file pointer FILE *f; char ch[100]; // open the file for read and write operation if((f=fopen("test.txt","r+"))==NULL){ //if the file does not exist print the string printf("Cannot open the file..."); exit(1); } for(int i=0;i<10;i++){ //enter the strings with values in the file fprintf(f,"The count number is %dn",i+1); } fclose(f); // open the file for read and write operation if((f=fopen("test.txt","r+"))==NULL){ //if the file does not exist print the string printf("Cannot open the file..."); exit(1); } printf("File content is--n"); printf("n...............print the strings..............nn"); while(!feof(f)){ //takes the first 100 character in the character array fgets(ch,100,f); //and print the strings printf("%s",ch); } //close the file fclose(f); return 0; }
通过 fprintf 函数将数据写入到文件中,在通过 fgets 函数读取文件的每一行数据;
三.猜你喜欢- C 语言 数组下标越界和内存溢出区别
- C 语言 使用指针遍历数组
- C 语言 指针和数组区别
- C 语言 指针数组和数组指针区别
- C 语言 野指针
- C 语言 函数值传递和址传递
- C 语言 函数不定长参数
- C 语言 函数指针
- C 语言 指针函数
- C 语言 回调函数 callback
- C 语言 #pragma once
- C 语言 #include <> 与 #include “” 区别
- C 语言 const 修饰函数参数
- C 语言 const 和 define 区别
- C 语言 #运算符
- C 语言 ##运算符
- C 语言 __VA_ARGS__
- C 语言 ##__VA_ARGS__
- C 语言 函数不定长参数 ##__VA_ARGS__经典案例
- C 语言 va_start / va_end / va_arg 自定义 printf 函数
- C 语言 main 函数
- C 语言 main 函数参数 main(int argc, char *argv[])
- C 语言 局部变量
- C 语言 全局变量
- C 语言 全局变量和局部变量区别
- C 语言 static
- C 语言 extern
未经允许不得转载:猿说编程 » C 语言 fprintf 函数



