#include "stdafx.h"
#include
#include
#include
struct STUDENT{
int Number;
char Name[32];
int Class;
float Score;
};
#define IS_WHITE(ch) ((unsigned char)ch<=15 || ch==' ')
//trim函数去除字符串str前后的空白字符
char *trim(char *str)
{
char ch;
char *p, *q, *r;
p=str; q=str; r=str-1;
while(1)
{
ch=*p;
if (!IS_WHITE(ch)) break;
if (ch==0) break;
p++;
}
while(1)
{
ch=*p++;
*q=ch;
if (ch==0) break;
if (!IS_WHITE(ch)) r=q;
q++;
}
*++r=0;
return str;
}
int InputStudent(const char *filename)
{
FILE *f;
int n;
char str[200];
struct STUDENT student;
f=fopen(filename,"wb");
if (f==NULL) return 0; //返回0表示失春羡旁败
for (n=0; ; ++n)
{
printf("请输入学号: "); scanf("%d", &student.Number);
if (student.Number==0) break;
while(1)
{
printf("请输入姓名:"); gets(str);
trim(str);
if (str[0]==0) continue; //确保姓名不为空
strncpy(student.Name, str, sizeof(student.Name)-1);
//上句确保输入扒橡姓名长度不超过31字节
break;
}
printf("请输入班级:\n"); scanf("%d", &student.Class);
printf("请输入成绩:\n"); scanf("%f", &student.Score);
fwrite(&student, sizeof(student),1, f);
// printf("%08d %-32s %-3d %f\n",
// student.Number,
// student.Name,
// student.Class,
// student.Score);
}
fclose(f);
return n; //返回有多少个学生信息录入
}
void SortStudent(struct STUDENT *student, int n)
{
int i,j;
struct STUDENT *a, *b, t;
for (j=0; j{
a=student+j;
for (i=j+1; i{
b=student+i;
if (a->Number<=b->Number) continue; //否则交换
memcpy(&t,a,sizeof(struct STUDENT));
memcpy(a,b,sizeof(struct STUDENT));
memcpy(b,&t,sizeof(struct STUDENT));
派高}
}
}
void PrintStudent(const struct STUDENT *student, int n)
{
int i;
for (i=0; iprintf("%08d %-32s %-3d %6.1f\n",
student->Number,
student->Name,
student->Class,
student->Score);
}
void main()
{
int n1, n2, n;
struct STUDENT *student;
const char fn1[]="d:\\student1.bin"; //文件1
const char fn2[]="d:\\student2.bin"; //文件2
const char fn[]="d:\\student.bin"; //合并后的文件
FILE *f;
printf("学生信息录入(录入过程中,学号输入为0,则退出录入)\n");
printf("第一步:%s中的学生信息录入:\n", fn1);
n1=InputStudent(fn1);
if (0==n1)
{
printf("%s中的学生信息录入有误,退出程序\n",fn1);
return;
}
printf("第二步:%s中的学生信息录入:\n", fn2);
n2=InputStudent(fn2);
if (0==n2)
{
printf("%s中的学生信息录入有误,退出程序\n",fn2);
return;
}
printf("第三步:合并%s, %s\n",fn1,fn2);
student=(struct STUDENT *)malloc((n1+n2)*sizeof(struct STUDENT));
f=fopen(fn1,"rb");
if (f==NULL) {printf("%s打开失败,退出程序\n",fn1); return;}
n=fread(student,sizeof(struct STUDENT), n1, f);
fclose(f);
if (n!=n1) {printf("%s读入错误,退出程序\n",fn1); return;}
f=fopen(fn2,"rb");
if (f==NULL) {printf("%s打开失败,退出程序\n",fn2); return;}
n=fread(student+n1,sizeof(struct STUDENT), n2, f);
// PrintStudent(student,n1+n2); //输出学生信息
fclose(f);
if (n!=n2) {printf("%s读入错误,退出程序\n",fn2); return;}
n=n1+n2;
SortStudent(student,n);
f=fopen(fn,"wb");
if (f==NULL) {printf("%s建立失败,退出程序\n",fn); return;}
fwrite(student, sizeof(struct STUDENT), n, f);
fclose(f);
PrintStudent(student,n); //输出学生信息
free(student);
}
1、使用文件操作函数打开文件,读取信息,重新写入并覆盖原文件,写的时候当遇到需要合并的两行时,忽略两之间的换行符。
2、例程:
#include
#include
#define K 5 //合并第六行第七行
FILE *p;//文件指针
int i,j;
char a[1000][1000];
int main(){
p = fopen("文件名称", "尺核纳r");//第一个字符串是你文件路径,路径中的\要写成\\,文件名必须带后缀
for(i=0;fgets(s[i],1000,p)!=EOF;i++)//EOF为文件尾(end of file),读取文本陵没中的数据
merge(); //合并操作
fclose(p);
p = fopen("文件名称", "w");//以写入方式重新打开
for(j=0;j fprintf(p,s[j]);
if(j!=K)fprintf(p,"\n"); //当不为合并行时,输出一个换行符
}
氏友 fclose(p)
return 0;
}
struct imfomation{
char Student No;
int name;
int class;
int score;
};
#include "stdio.h"
void main()
{
struct imfomation a;/拿唤/a为结构体变量
printf("请输入学号:\n");
gets(a.name);
printf("请输入名字:\n");
scanf("%d",&a.age);
printf("请输入班级:\n");
printf("请输入成绩:\n");
scanf("激稿%d",&a.salary);
printf("学号:%s\n名字:%d\n班级:%d\n"消铅凯:"成绩:%s\n,a.Student No,a. name,a.class ,a.scoreNo);
}