前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c语言中fprintf_c语言输出函数printf

c语言中fprintf_c语言输出函数printf

作者头像
全栈程序员站长
发布2022-11-07 16:51:23
2.5K0
发布2022-11-07 16:51:23
举报
文章被收录于专栏:全栈程序员必看

目录

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

一.fprintf 函数简介

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

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

int fprintf (FILE* stream, const char*format, [argument]);

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

代码语言:javascript
复制
//示例:
fprintf(stream,"www.codersrc.com\n");
fprintf(stream,"www.codersrc.com age:%d\n",17);
fprintf(stream,"www.codersrc.com age:%d name:%s\n",17, "zhangsan");
fprintf(stream,"www.codersrc.com age:%d name:%s height:%f\n",17, "zhangsan",1.75);

二.fprintf 函数使用

代码语言:javascript
复制
/******************************************************************************************/
//@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 %d\n",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..............\n\n");
    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语言输出函数printf
c语言中fprintf_c语言输出函数printf

三.猜你喜欢

  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://javaforall.cn/183459.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一.fprintf 函数简介
  • 二.fprintf 函数使用
  • 三.猜你喜欢
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档