#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;
}
其中: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;
}