将一些格式化的数据写入文本文件,…
2014-01-13 16:09
183 查看
#include<stdio.h>
main()
{
FILE *fp;
int i;
struct
stu
//定义结构体类型
{
char name[15];
char num[6];
float score[2];
}student;
//说明结构体变量
if((fp=fopen("test.txt","w"))==NULL)
//以文本只写方式打开文件
{
printf("can't open file,strike any key
exit!");
exit(0);
}
printf("input data:\n");
for(i=0;i<2;i++)
{
scanf("%s%s%f%f",student.name,student.num,&student.score[0],&student.score[1]);
//从键盘上输入
fprintf(fp,"%s%s%7.2f%7.2f",student.name,student.num,student.score[0],student.score[1]);
}
//写入文件
fclose(fp);
//关闭文件
if((fp=fopen("test.txt","r"))==NULL)
//以文本只读方式重新打开文件
{
printf("cannot open
file");
exit(0);
}
printf("output from file:\n");
while(fscanf(fp,"%s%s%f%f\n",student.name,student.num,&student.score[0],&student.score[1])!=EOF);//从文件读入
printf("%s %s %7.2f
%7.2f\n",student.name,student.num,student.score[0],student.score[1]);
//显示到屏幕
fclose(fp);
main()
{
FILE *fp;
int i;
struct
stu
//定义结构体类型
{
char name[15];
char num[6];
float score[2];
}student;
//说明结构体变量
if((fp=fopen("test.txt","w"))==NULL)
//以文本只写方式打开文件
{
printf("can't open file,strike any key
exit!");
exit(0);
}
printf("input data:\n");
for(i=0;i<2;i++)
{
scanf("%s%s%f%f",student.name,student.num,&student.score[0],&student.score[1]);
//从键盘上输入
fprintf(fp,"%s%s%7.2f%7.2f",student.name,student.num,student.score[0],student.score[1]);
}
//写入文件
fclose(fp);
//关闭文件
if((fp=fopen("test.txt","r"))==NULL)
//以文本只读方式重新打开文件
{
printf("cannot open
file");
exit(0);
}
printf("output from file:\n");
while(fscanf(fp,"%s%s%f%f\n",student.name,student.num,&student.score[0],&student.score[1])!=EOF);//从文件读入
printf("%s %s %7.2f
%7.2f\n",student.name,student.num,student.score[0],student.score[1]);
//显示到屏幕
fclose(fp);
相关文章推荐
- Java在硬盘上创建一个文件并写入一些文字数据FileWriter
- 将数据写入到文本文件中
- C语言sprintf()函数:将格式化的数据写入字符串
- 如何用C语言产生10000个不重复的随机整数?并且把它写入到一个文本文件中作为其他函数测试用的数据。
- Matlab处理读取ASCII文本数据找到规则需求,重新写入数据到新的文本文件[示例:有限元边界条件施加]
- C语言sprintf()函数:将格式化的数据写入字符串
- sprintf()函数:将格式化的数据写入字符串
- 在 SQLite 和 ListView之间格式化一些数据
- 【转载】一些数据格式化-Eval( " ")和DataBinder.Eval(Container.DataItem, " ")的区别及用法&DataFormatString使用笔记
- Spark SQL将数据写入Mysql表的一些坑
- C语言snprintf()函数:将格式化的数据写入字符串—sprintf()
- sprintf 编辑 sprintf指的是字符串格式化命令,主要功能是把格式化的数据写入某个字符串中。sprintf 是个变参函数。使用sprintf 对于写入buffer的字符数是没有
- 向文本文件中写入16进制数据
- 一些数据格式化-Eval( " ")和DataBinder.Eval(Container.DataItem, " ")的区别及用法
- 类--写入一行或多行数据--日期格式化工具类 4000
- 使用Apache POI读取Excel文件,将读取的数据转换成json写入文本文件
- Vb6.0写文本文件并写入十六进制数据
- C语言sprintf()函数:将格式化的数据写入字符串
- JAVA实现向文本文件中多次写入数据
- EXCEL:读取文本文件到EXCEL并对数据列进行格式化