#if 0
#include<iostream>#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
ofstream outfile("client.dat",ios::out);
if(!outfile)
{
cout<<"can not open the file "<<endl;
exit(0);
}
cout<<"entr the account,name,balance.\n"
<<"enter end_of_file to end input.\n";
int account;
char name[30];
float balance;
while(cin>>account>>name>>balance)
{
outfile<<account<<' '<<name<<' '<<balance<<'\n';
cout<<"?";
}
return 0;
}
#endif
/
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
ifstream inthefile("client.dat",ios::in);
if(!inthefile)
{
cout <<"can not open the file"<<endl;
exit(0);
}
int account;
char name[30];
float balance;
cout<<setiosflags(ios::left)<<setw(13)<<"Account"<<setw(13)
<<"Name"<<"Balance"<<endl;
while(inthefile>>account>>name>>balance)
{
cout<<setiosflags(ios::left)<<setw(13)<<account<<setw(13)
<<name<<balance<<endl;
}
return 0;
}
#endif
#if 0
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
struct Student
{
int id;
char name[20];
};
int main()
{
ofstream outthefile("student.dat",ios::ate);
if(!outthefile)
{
cout<<"the file can not open!"<<endl;
exit(0);
}
Student student;
cout<<"enter the id(1-100)"<<endl;
cin>>student.id;
while(student.id >0&&student.id<=100)
{
cout<<"Enter the name: "<<endl;
cin>>student.name;
outthefile.seekp((student.id-1)*sizeof(Student));
outthefile.write(reinterpret_cast<char*>(&student),sizeof(student));
cout<<"enter the number"<<endl;
cin>>student.id;
}
return 0;
}
#endif
//随机写文件
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>
using namespace std;
struct Student
{
int id;
char name[20];
};
int main()
{
ifstream inthefile("student.dat",ios::in);
if(!inthefile)
{
cerr<<"the file can not open!"<<endl;
exit(1);
}
cout<<setiosflags(ios::left)<<setw(13)<<"Id"<<"Name"<<endl;
Student student;
inthefile.read(reinterpret_cast<char *>(&student),sizeof(student));
while(inthefile&&!inthefile.eof())
{
if(student.id !=0)
cout<<setiosflags(ios::left)<<setw(13)<<student.id<<student.name<<endl;
inthefile.read(reinterpret_cast<char *>(&student),sizeof(student));
}
return 0;
}
#endif
//随机读文件
#if 0
#include<stdio.h>
int main()
{
FILE *in,*out;
in = fopen("main.cpp","r");
if(in==NULL)
{
printf("the file open error!\n");
return 0;
}
out = fopen("copyfile.dat","w");
if(out==NULL)
{
printf("file open error!\n");
return 0;
}
while(!feof(in))
{
fputc(fgetc(in),out);
}
fclose(in);
fclose(out);
return 0;
}
#endif
//C中fgetc()和fputc()的使用
#if 0
#include<stdio.h>
#define N 3
struct Student
{
int id;
char name[10];
int age;
}stu[N];
void writefile()
{
FILE *fp;
if((fp=fopen("studentData.dat","w"))==NULL)
{
printf("can not open the file\n");
}
printf("Enter the information of students:(id name sex):\n");
for(int i =0; i<N;i++)
{
scanf("%d%s%d",&stu[i].id,stu[i].name,&stu[i].age);
}
for(int i = 0; i<N;i++)
{
fwrite(&stu[i],sizeof(struct Student),1,fp);
}
fclose(fp);
}
void readfile()
{
FILE *fp;
fp = fopen("studentData.dat","r");
if(fp ==NULL)
{
printf("can not open the file!\n");
}
printf("id Name Age\n");
for(int i =0; i<N;i++)
{
if(fread(&stu[i],sizeof(struct Student),1,fp)!=1)
{
printf("open error!\n");
}
else
printf("%d %s %d\n",stu[i].id,stu[i].name,stu[i].age);
}
fclose(fp);
}
int main()
{
writefile();
printf("Printf the information of student;\n");
readfile();
return 0;
}
#endif
//C中的随机访问文件
#if 0
#include<stdio.h>
#include<time.h>
#include<string.h>
int main()
{
char show[20];
time_t Time;
struct tm *Val;
printf("显示时间:\n");
while(1)
{
time(&Time);
Val = localtime(&Time);
strftime(show,20,"%y-%m-%d %H:%M:%S",Val);
printf("%s\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b",show);
}
return 0;
}
#endif
#if 1
#include<iostream>
#include<cstdlib>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream infile("in.txt",ios::in);
if(!infile)
{
cout<<"the in file can not open!"<<endl;
exit(0);
}
ofstream outfile("out.txt",ios::out);
if(!outfile)
{
cout<<"the out file can not open!"<<endl;
exit(1);
}
string str;
while(infile>>str)
{
outfile<<str;
}
return 0;
}
#endif
//文本文件的拷贝