目錄
- 一.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,"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 函式使用
/******************************************************************************************/
//@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 語言 陣列下標越界和記憶體溢位區別
- 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 函式
本文由博客 - 猿說編程 猿說編程 發布!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/335087.html
標籤:其他
下一篇:基于注解的自動配置
