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浮點數(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
三、實驗結(jié)果
四、參考文獻
C語言fscanf 和 fprintf函數(shù)-例程C代碼
c語言fprintf、fscanf、sscanf以及sprintf函數(shù)知識要點總結(jié)
總結(jié)
本文僅僅簡單介紹了【C語言】fscanf 和 fprintf函數(shù)驗證,評論區(qū)歡迎討論。
到此這篇關(guān)于 C語言 fscanf 和 fprintf函數(shù)示例詳解的文章就介紹到這了,更多相關(guān) C語言 fscanf 和 fprintf函數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于OpenCV實現(xiàn)的人臉簽到系統(tǒng)源代碼
本文從實際背景和需求出發(fā),采用人臉識別簽到考勤改變了傳統(tǒng)人工檢驗的做法,極大提高了組織效率和辦事能力,這篇文章主要給大家介紹了關(guān)于如何基于OpenCV實現(xiàn)的人臉簽到系統(tǒng)的相關(guān)資料,需要的朋友可以參考下2024-04-04C語言基于循環(huán)鏈表解決約瑟夫環(huán)問題的方法示例
這篇文章主要介紹了C語言基于循環(huán)鏈表解決約瑟夫環(huán)問題的方法,簡單描述了約瑟夫環(huán)問題并結(jié)合實例形式分析了C語言使用循環(huán)鏈表解決約瑟夫環(huán)問題的具體操作技巧,需要的朋友可以參考下2018-01-01