文件输入输出实验

#c++文件输入输出小程序

#include
#include
#include
using namespace std;

void student(){
ofstream outfile(“F:\student.txt”);
char name[12],id[8];
int math,eng,computer;
for(int i=0;i<3;i++){
cout<<“输入姓名:”; cin>>name;
cout<<“输入身份证号:”; cin>>id;
cout<<“输入数学成绩:”; cin>>math;
cout<<“输入英语成绩:”; cin>>eng;
cout<<“输入计算机成绩:”; cin>>computer;
outfile<<name<<" “<<id<<” “<< math<<” "
<<eng<<" "<<computer<<endl;
}
outfile.close();

}

void getstudent(){
ifstream infile(“F:\student.txt”);
char name[12],id[8];
int math,eng,computer,sum;
cout<<setw(10)<<“姓名”<<setw(10)<<“身份证号”<<setw(10)<<“数学成绩”
<<setw(10)<<“英语成绩”<<setw(10)<<“计算机成绩”<<setw(10)<<“总分”<<endl;
infile>>name;
while(!infile.eof()){
infile>>id>>math>>eng>>computer;
sum=math+eng+computer;
cout<<setw(10)<<name<<setw(10)<<“id”<<setw(10)<<math
<<setw(10)<<eng<<setw(12)<<computer<<setw(10)<<sum<<endl;

	infile>>name;

}
infile.close();

}

int main(){
student();
getstudent();
return 0;
}

结果如上

#文件复制
#include
#include
#include
#include
using namespace std;

const int BUFSIZE = 1024 * 1024;

void CopyRawFile(string InFile, string OutFile)
{
char* pchar = new char[BUFSIZE];

 ofstream ofile;
 ifstream ifile;

 ofile.open(OutFile.c_str(), ios::binary);
 ifile.open(InFile.c_str(), ios::binary);

 while(ifile.read(pchar, BUFSIZE))
     ofile.write(pchar, BUFSIZE);

 ofile.write(pchar, ifile.gcount());
 ifile.close();
 ofile.close();
 delete []pchar;

}

}

int main(){
CopyRawFile(“F:\文件\student.txt”,“F:\文件\huge.txt”);
return 0;
}

生成了1个文档
其中:ofstream fout(filename.c_str());
c_str是string类的一个函数,可以把string类型变量转换成char变量
open()要求的是一个char
字符串

C++ primer plus第六版第六题
#include
#include
#include
#include
using namespace std;
int main(){
int num;
struct student{
char hu[20];
int dd;
}a;
ifstream in(“F:\123.txt”);
in>>num;
for(int i=0;i<num;i++){
in>>a.hu>>a.dd;
cout<<a.hu<<endl;
cout<<a.dd<<endl;
/a.hu=“11”;
a.dd=0;
/
}
in.close;
return 0;

}

不能用空格

#include
#include
#include
#include

using namespace std;
const int SIZE = 60;
const double stand = 10000.0;

struct patron{
string name;
double money;
};

int main()
{
using namespace std;
char filename[SIZE];
ifstream inFile;

//------打开文件------
cout << "Enter name of data file: ";
cin.getline(filename, SIZE);
inFile.open(filename);
if (!inFile.is_open())
{
cout << "Could not open the file " << filename << endl;
cout << “Program terminating.\n”;
exit(EXIT_FAILURE);
}

//------读取数据------
int count;
inFile >> count;
inFile.get();//读取文件数据

patron * peoplelist = new patron[count];
for(int i = 0; i < count  ;i++)
{
    getline(inFile, peoplelist[i].name);

    inFile >> peoplelist[i].money;
    inFile.get();       

}   

//------输出数据------
cout << "\nGrand Patrons: " << endl;
int n = 0;
for(int i = 0; i < count ;i++)
{
if(peoplelist[i].money > stand)
{
cout << "Patrons name: " << peoplelist[i].name << endl;
cout << "donation: " << peoplelist[i].money << endl;
n++;
}
}
if(n == 0)
cout << “none”;

int m = 0;
cout << "\nPatrons: " << endl;      
for(int i = 0; i < count ;i++)
{
    if(peoplelist[i].money <= stand)
    {
        cout << "Patrons name: " << peoplelist[i].name << endl;
        cout << "donation: " << peoplelist[i].money << endl;
        m++;
    }
}           
if(m == 0)
    cout << "none";   

//----释放内存,关闭文件----
delete peoplelist;
inFile.close();
return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值