700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > C语言 fprintf 函数 - C语言零基础入门教程

C语言 fprintf 函数 - C语言零基础入门教程

时间:2022-05-09 03:59:50

相关推荐

C语言 fprintf 函数 - C语言零基础入门教程

目录

一.fprintf 函数简介二.fprintf 函数使用三.猜你喜欢

零基础 C/C++ 学习路线推荐 : C/C++ 学习目录 >> C 语言基础入门

一.fprintf 函数简介

fprintf 是 C / C++ 中的一个格式化库函数,位于头文件 中,其作用是格式化输出到一个流文件中;函数原型为

/**描述:fputs 函数是向指定的文件写入一个字符串**参数:* [in] stream: 文件指针句柄;* [in] format: 格式化字符串,与 printf 函数一样;**返回值:如果成功,该函数返回一个非负值,如果发生错误则返回 EOF(-1)。*/int fprintf (FILE* stream, const char*format, [argument]);

fprintf 函数是变参函数,format 可以由一个或者多个参数构成,案例如下:

//示例:fprintf(stream,"\n");fprintf(stream," age:%d\n",17);fprintf(stream," age:%d name:%s\n",17, "zhangsan");fprintf(stream," age:%d name:%s height:%f\n",17, "zhangsan",1.75);

二.fprintf 函数使用

/******************************************************************************************///@Author:猿说编程//@Blog(个人博客地址): //@File:C语言教程 - C语言 fprintf 函数//@Time:/07/30 07:30//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!/******************************************************************************************/#include <cstdio>#include<stdio.h>#include<stdlib.h>int main(){//Initialize the file pointerFILE *f;char ch[100];// open the file for read and write operationif((f=fopen("test.txt","r+"))==NULL){//if the file does not exist print the stringprintf("Cannot open the file...");exit(1);}for(int i=0;i<10;i++){//enter the strings with values in the filefprintf(f,"The count number is %d\n",i+1);}fclose(f);// open the file for read and write operationif((f=fopen("test.txt","r+"))==NULL){//if the file does not exist print the stringprintf("Cannot open the file...");exit(1);}printf("File content is--\n");printf("\n...............print the strings..............\n\n");while(!feof(f)){//takes the first 100 character in the character arrayfgets(ch,100,f);//and print the stringsprintf("%s",ch);}//close the filefclose(f);return 0;}

通过 fprintf 函数将数据写入到文件中,在通过 fgets 函数读取文件的每一行数据;

三.猜你喜欢

C 语言 数组下标越界和内存溢出区别C 语言 使用指针遍历数组C 语言 指针和数组区别C 语言 指针数组和数组指针区别C 语言 野指针C 语言 函数值传递和址传递C 语言 函数不定长参数C 语言 函数指针C 语言 指针函数C 语言 回调函数 callbackC 语言 #pragma onceC 语言 #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 语言 staticC 语言 extern

未经允许:猿说编程 » C 语言 fprintf 函数

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。