当前位置: 代码网 > it编程>编程语言>C/C++ > C语言 fscanf 和 fprintf函数示例详解

C语言 fscanf 和 fprintf函数示例详解

2024年12月13日 C/C++ 我要评论
使用工具:1.编译器:devc2.c primer plus 第六版-1提示:以下是本篇文章正文内容,下面案例可供参考一、定义 int __cdecl fprintf(file * __restri

使用工具:
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函数内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com