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)文章
C++中的while循環(huán)和for循環(huán)語句學(xué)習(xí)教程
這篇文章主要介紹了C++中的while循環(huán)和for循環(huán)語句學(xué)習(xí)教程,是C++入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-09-09
C++中string轉(zhuǎn)換為char*類型返回后亂碼問題解決
這篇文章主要介紹了C++中string轉(zhuǎn)換為char*類型返回后亂碼問題解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
C/C++之動態(tài)內(nèi)存管理方式(new delete)
C++中new/delete集成內(nèi)存分配與對象構(gòu)造析構(gòu),是面向?qū)ο髢?nèi)存管理的核心,與malloc/free相比,支持類型安全、異常處理及數(shù)組操作,需嚴格配對使用,現(xiàn)代開發(fā)推薦智能指針替代,以提升安全性與自動化程度2025-09-09
c++函數(shù)中的指針參數(shù)與地址參數(shù)區(qū)別介紹
c++函數(shù)中的指針參數(shù)與地址參數(shù)區(qū)別介紹;可供參考2012-11-11

