大度的行为:C语言编程高手进啊!!!急啊!

来源:百度文库 编辑:高考问答 时间:2024/04/29 20:08:11
#include<stdio.h>

struct stu
{char name;
char num;
char sex;
int age;
float score;
struct stu *next;
}stu;
FILE *fp,*fp1;

main()
{ long int i,j;
char ch,y;
if((fp=fopen("student.txt","rb+"))==NULL||(fp1=fopen("student1.txt","wb+"))==NULL)
{
printf("\nopen score.txt was failed!");
getch();
exit(1);
}
printf("Please input the number of the student to delete:");
scanf("%ld",&i);
while(fread(&stu,sizeof(stu),1,fp)==1)
{
j=stu.num;
if(i==j)
{
printf("Is the information you want to delete?\n");
printf("number:%ld\n,name:%s\n,sex:%s\n,age:%d\n,score:%f\n",stu.num,stu.name,stu.sex,stu.age,stu.score);
}
}
printf("Do you really want to delete the information?(y/n)");
getchar();
ch=getchar();
if(ch=='y')
{while((fread(&stu,sizeof(stu),1,fp)==1))
{
j=stu.num;
if(j==i)
;
else
fwrite(&stu,sizeof(stu),1,fp1);
}
}
fclose(fp);
fclose(fp1);
remove("student.txt");
rename("student1.txt","student.txt");
printf("Delete was succesful!");
getchar();
}

这段程序中有两个文件读写函数fread()和fwrite();
其中fread()可以读出文件fp(即student.txe),
而fwrite()确不能将读出的数据写到fp1(既student1.txt)中;
这是为什么啊?我头都想破了!
请高手指教啊!!!!!
(这段程序的主要意思是将文件student.txt中的某些数据删除!)

用这个方式读写文件可能比较繁琐
使用文件指针并不方便
你可以这样
#include <fstream>
using namespace std ;
ofstream fout ("123.txt");
ifstream fin ("456.txt")
int main()
{
int a , b , c ;
char m[100] ;
fin >> a >> m ; //从文件456.txt读入整数给a,读进字符串给m
fout << a << m ;//输出a的数值和m这个字符串到文件123.txt
//只要有输入文件,就可以
//无需要建立输出文件
}