使用工具:
1.编译器:devc
2.c primer plus 第六版-1
提示:以下是本篇文章正文内容,下面案例可供参考
一、定义
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 之间的整数(包含 0 和 999)
//rand()%1000/100为0-9.99浮点数
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];//定义一个字符型数组去存
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]);//赋值于字符型数组中的元素值
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
三、实验结果





四、参考文献
c语言fscanf 和 fprintf函数-例程c代码
c语言fprintf、fscanf、sscanf以及sprintf函数知识要点总结
总结
本文仅仅简单介绍了【c语言】fscanf 和 fprintf函数验证,评论区欢迎讨论。
到此这篇关于 c语言 fscanf 和 fprintf函数示例详解的文章就介绍到这了,更多相关 c语言 fscanf 和 fprintf函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论