C与C++的文件简单操作

微笑

#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

/


#if 0
#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

//随机写文件


#if 0
#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

//文本文件的拷贝

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值