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

目录

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

一.fprintf 函数简介

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

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

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 <cstdio>
#include<stdio.h>
#include<stdlib.h>
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语言 fprintf 函数 - C语言零基础入门教程

三.猜你喜欢

  1. C 语言 数组下标越界和内存溢出区别
  2. C 语言 使用指针遍历数组
  3. C 语言 指针和数组区别
  4. C 语言 指针数组和数组指针区别
  5. C 语言 野指针
  6. C 语言 函数值传递和址传递
  7. C 语言 函数不定长参数
  8. C 语言 函数指针
  9. C 语言 指针函数
  10. C 语言 回调函数 callback
  11. C 语言 #pragma once
  12. C 语言 #include <> 与 #include “” 区别
  13. C 语言 const 修饰函数参数
  14. C 语言 const 和 define 区别
  15. C 语言 #运算符
  16. C 语言 ##运算符
  17. C 语言 __VA_ARGS__
  18. C 语言 ##__VA_ARGS__
  19. C 语言 函数不定长参数 ##__VA_ARGS__经典案例
  20. C 语言 va_start / va_end / va_arg 自定义 printf 函数
  21. C 语言 main 函数
  22. C 语言 main 函数参数 main(int argc, char *argv[])
  23. C 语言 局部变量
  24. C 语言 全局变量
  25. C 语言 全局变量和局部变量区别
  26. C 语言 static
  27. C 语言 extern

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

本文由博客 - 猿说编程 猿说编程 发布!

原文链接: https://www.cnblogs.com/shuopython/p/15456003.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

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

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/214524

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月13日 上午2:20
下一篇 2023年2月13日 上午2:20

相关推荐