给定程序中,函数fun的功能是将参数给定的字符串,整数,浮点数写到文本文件中,再用字符串方式从此文本文件中逐个读入,并调用库函数atoi和atof将字符串转化成相应的整数、浮点数,然后将其显示在屏幕上。
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
void fun(char *s, int a, double f)
{
FILE *fp;
char str[100], str1[100], str2[100];
int a1;
double f1;
fp = fopen("file1.txt", "w");
fprintf(fp, "%s %d %f", s, a, f);
fclose(fp);
fp = fopen("file1.txt", "r");
fscanf(fp, "%s%s%s", str, str1, str2);
fclose(fp);
a1 = atoi(str1);
f1 = atof(str2);
printf("%s %s %s\n", str, str1, str2);
printf("\nThe result:\n\n%s %d %f\n", str, a1, f1);
}
int main()
{
char a[10] = "Hello!";
int b = 12345;
double c = 98.76;
fun(a, b, c);
getchar();
return 0;
}