当前位置: 首页 > article >正文

【C语言】fscanf 和 fprintf函数

【C语言】fscanf 和 fprintf函数


文章目录

    • @[TOC](文章目录)
  • 前言
  • 一、定义
  • 二、代码例程
  • 三、实验结果
  • 四、参考文献
  • 总结

前言

使用工具:
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函数验证,评论区欢迎讨论。


http://www.kler.cn/a/429017.html

相关文章:

  • 【Qt移植LVGL】QWidget手搓LVGL软件仿真模拟器(非直接运行图形库)
  • 用Python开发一个经典贪吃蛇小游戏
  • 【Stable Diffusion】ComfyUI 基础教程-环境部署和插件安装
  • MQ 队列 的 通信过程
  • SpringBoot整合Mockito进行单元测试超全详细教程 JUnit断言 Mockito 单元测试
  • String IP和Int IP的互相转换
  • HTML旋转爱心(完整代码)
  • lanqiaoOJ 3744:小蓝的智慧拼图购物 ← pair+优先队列
  • Origin快速拟合荧光寿命、PL Decay (TRPL)数据分析处理-方法二
  • Apache Solr 身份认证绕过漏洞复现(CVE-2024-45216)
  • MySQL系列之数据授权(安全)
  • 前端导出excel实战(xlsx库和exceljs库)
  • Leetcode 739-每日温度
  • Docker容器网络与通信
  • mysql备份数据库
  • 8.16DEBUG——DOCKER相关,DOCKER启动异常
  • Python-分析内存进制转换
  • HOC vs Render Props vs Hooks
  • 在Windows下C语言获取当前应用程序运行路径并获取指定目录下所有文件(包括子目录)
  • 决策树:ID3、C4.5和CART特征选择方式