C語言 fscanf 和 fprintf函數(shù)示例詳解
使用工具:
1.編譯器:DEVC
2.C Primer Plus 第六版-1
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、定義
int __cdecl fprintf(FILE * __restrict__ _File,const char * __restrict__ _Format,...); int __cdecl printf(const char * __restrict__ _Format,...); int __cdecl sprintf(char * __restrict__ _Dest,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN; int __cdecl vfprintf(FILE * __restrict__ _File,const char * __restrict__ _Format,va_list _ArgList); int __cdecl vprintf(const char * __restrict__ _Format,va_list _ArgList); int __cdecl vsprintf(char * __restrict__ _Dest,const char * __restrict__ _Format,va_list _Args) __MINGW_ATTRIB_DEPRECATED_SEC_WARN; int __cdecl fscanf(FILE * __restrict__ _File,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN; int __cdecl scanf(const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN; int __cdecl sscanf(const char * __restrict__ _Src,const char * __restrict__ _Format,...) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
二、代碼例程
main.c
#include <stdio.h> #include <stdlib.h> #include "file.h" /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char *argv[]) { writeTest(1000); readData(1000); return 0; }
file.c
#include "file.h" void writeTest(int data) { FILE *fp1,*fp2; double r1,r2; int i; srand((unsigned)time(NULL)); //若為空則退出 if((fp1=fopen("D:\\in1.txt","w"))==NULL) { printf("can not open the in file\n"); exit(0); } if((fp2=fopen("D:\\out1.txt","w"))==NULL) { printf("can not open the out file\n"); exit(0); } //RAND_MAX 32767 rand()%1000——0到 999 之間的整數(shù)(包含 0 和 999) //rand()%1000/100為0-9.99浮點(diǎn)數(shù) for(i=0;i<data;i++) { r1=rand()%1000/100.0; r2=rand()%1000/100.0; fprintf(fp1,"%lf %lf\n",r1,r2); fprintf(fp2,"%lf \n",r1+r2); } fclose(fp1); fclose(fp2); } void readData(int data) { FILE *fp1,*fp2; int i,j; double arr[10];//定義一個字符型數(shù)組去存 if((fp1=fopen("D:\\in1.txt","r"))==NULL){ printf("can not open the in file\n"); exit(0); } for(i=0;i<data;i++) for(j=0; j<2; j++) fscanf(fp1,"%lf ",&arr[0]);//賦值于字符型數(shù)組中的元素值 printf("%lf \n",arr[0]);//在終端中打印輸出 fclose(fp1); if((fp2=fopen("D:\\out1.txt","r"))==NULL){ printf("can not open the out file\n"); exit(0); } for(i=0;i<data;i++) for(j=0; j<1; j++) fscanf(fp2,"%lf",&arr[1]); printf("%lf",arr[1]);//在終端中打印輸出 fclose(fp2); }
file.h
#ifndef __FILE_H #define __FILE_H #include <stdio.h> #include <stdlib.h> #include "math.h" typedef unsigned char uint8; #endif
三、實(shí)驗(yàn)結(jié)果
四、參考文獻(xiàn)
C語言fscanf 和 fprintf函數(shù)-例程C代碼
c語言fprintf、fscanf、sscanf以及sprintf函數(shù)知識要點(diǎn)總結(jié)
總結(jié)
本文僅僅簡單介紹了【C語言】fscanf 和 fprintf函數(shù)驗(yàn)證,評論區(qū)歡迎討論。
到此這篇關(guān)于 C語言 fscanf 和 fprintf函數(shù)示例詳解的文章就介紹到這了,更多相關(guān) C語言 fscanf 和 fprintf函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C讀txt到二維數(shù)組的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狢讀txt到二維數(shù)組的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12基于OpenCV實(shí)現(xiàn)的人臉簽到系統(tǒng)源代碼
本文從實(shí)際背景和需求出發(fā),采用人臉識別簽到考勤改變了傳統(tǒng)人工檢驗(yàn)的做法,極大提高了組織效率和辦事能力,這篇文章主要給大家介紹了關(guān)于如何基于OpenCV實(shí)現(xiàn)的人臉簽到系統(tǒng)的相關(guān)資料,需要的朋友可以參考下2024-04-04C++?opencv圖像處理實(shí)現(xiàn)灰度變換示例
這篇文章主要為大家介紹了C++?opencv圖像處理灰度變換的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05C語言基于循環(huán)鏈表解決約瑟夫環(huán)問題的方法示例
這篇文章主要介紹了C語言基于循環(huán)鏈表解決約瑟夫環(huán)問題的方法,簡單描述了約瑟夫環(huán)問題并結(jié)合實(shí)例形式分析了C語言使用循環(huán)鏈表解決約瑟夫環(huán)問題的具體操作技巧,需要的朋友可以參考下2018-01-01QT中QStringListModel類的應(yīng)用介紹
QStringListModel是最簡單的模型類,具備向視圖提供字符串?dāng)?shù)據(jù)的能力,本文主要介紹了QT中QStringListModel類的應(yīng)用介紹,具有一定的參考價值,感興趣的可以了解一下2024-01-01C++ 中二分查找遞歸非遞歸實(shí)現(xiàn)并分析
這篇文章主要介紹了C++ 中二分查找遞歸非遞歸實(shí)現(xiàn)并分析的相關(guān)資料,需要的朋友可以參考下2017-06-06