update
This commit is contained in:
18
practical_exercises/10_day_practice/day10/文件例题/12-1.cpp
Normal file
18
practical_exercises/10_day_practice/day10/文件例题/12-1.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
//用cin输入字符串数据时,如果字符串中含有空白就不能完整输入。因为遇到空白字符时,cin就认为字符串结束了。
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char a[50];
|
||||
cout<<"please input a string:";
|
||||
cin>>a;
|
||||
cout<<a<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
若a的内容是:
|
||||
this is a string!
|
||||
就难以输入啦!
|
||||
这样的数据应用输入流类的成员函数输入
|
||||
*/
|
||||
13
practical_exercises/10_day_practice/day10/文件例题/12-2应用.cpp
Normal file
13
practical_exercises/10_day_practice/day10/文件例题/12-2应用.cpp
Normal file
@@ -0,0 +1,13 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char stu[5][10];
|
||||
int i;
|
||||
for(i=0;i<5;i++)
|
||||
cin.getline(stu[i],10,',');
|
||||
for(i=0;i<5;i++)
|
||||
cout<<stu[i]<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
18
practical_exercises/10_day_practice/day10/文件例题/12-3.cpp
Normal file
18
practical_exercises/10_day_practice/day10/文件例题/12-3.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
//º¯ÊýÔÐÍ
|
||||
//put(char c)
|
||||
//write(const char*c, int n)
|
||||
int main(){
|
||||
char c;
|
||||
char a[50]="this is a string...";
|
||||
cout<<"use get() input char:";
|
||||
while((c=cin.get())!='\n'){
|
||||
cout.put(c);
|
||||
cout.put('\n');
|
||||
cout.put('t').put('h').put('i').put('s').put('\n');
|
||||
cout.write(a,12).put('\n');
|
||||
cout<<"look"<<"\t here!"<<endl;
|
||||
}
|
||||
system("pause");
|
||||
}
|
||||
17
practical_exercises/10_day_practice/day10/文件例题/12-5.cpp
Normal file
17
practical_exercises/10_day_practice/day10/文件例题/12-5.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
//Eg12-5.cpp
|
||||
#include<iostream>
|
||||
#include<iomanip>
|
||||
using namespace std;
|
||||
int main(){
|
||||
char c[30]="this is string";
|
||||
double d=-1234.8976;
|
||||
cout<<setw(30)<<left<<setfill('*')<<c<<"----L1"<<endl;
|
||||
cout<<setw(30)<<right<<setfill('*')<<c<<"----L2"<<endl;
|
||||
//showbase显示数值的基数前缀
|
||||
cout<<dec<<showbase<<showpoint<<setw(30)<<d<<"----L3"<<"\n";
|
||||
//showpoint显示小数点
|
||||
cout<<setw(30)<<showpoint<<setprecision(10)<<d<<"----L4"<<"\n";
|
||||
//setbase(8)设置八进制
|
||||
cout<<setw(30)<<setbase(16)<<100<<"----L5"<<"\n";
|
||||
system("pause");
|
||||
}
|
||||
28
practical_exercises/10_day_practice/day10/文件例题/12-6.cpp
Normal file
28
practical_exercises/10_day_practice/day10/文件例题/12-6.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
//12-6.cpp
|
||||
#include<iostream>
|
||||
#include<fstream>
|
||||
using namespace std;
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
fstream ioFile;
|
||||
ioFile.open("./a.dat",ios::out);
|
||||
ioFile<<"张三"<<" "<<76<<" "<<98<<" "<<67<<endl; //L3
|
||||
ioFile<<"李四"<<" "<<89<<" "<<70<<" "<<60<<endl;
|
||||
ioFile<<"王十"<<" "<<91<<" "<<88<<" "<<77<<endl;
|
||||
ioFile<<"黄二"<<" "<<62<<" "<<81<<" "<<75<<endl;
|
||||
ioFile<<"刘六"<<" "<<90<<" "<<78<<" "<<67<<endl;
|
||||
ioFile.close();
|
||||
ioFile.open("./a.dat",ios::in|ios::binary);
|
||||
char name[10];
|
||||
int chinese,math,computer;
|
||||
cout<<"姓名\t"<<"英语\t"<<"数学\t"<<"计算机\t"<<"总分"<<endl;
|
||||
ioFile>>name;
|
||||
while(!ioFile.eof()) {
|
||||
ioFile>>chinese>>math>>computer;
|
||||
cout<<name<<"\t"<<chinese<<"\t"<<math<<"\t"<<computer<<"\t"<<chinese+math+computer<<endl;
|
||||
ioFile>>name;
|
||||
}
|
||||
ioFile.close();
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
21
practical_exercises/10_day_practice/day10/文件例题/12-7.cpp
Normal file
21
practical_exercises/10_day_practice/day10/文件例题/12-7.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
//Eg12-7.cpp
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
int main(){
|
||||
char ch;
|
||||
ofstream out("/test.dat",ios::out|ios::binary); //L1
|
||||
for(int i=0;i<90;i++){
|
||||
if(i>0 && (i % 30)==0)
|
||||
out.put('\n');
|
||||
out.put(i);
|
||||
out.put(' ');
|
||||
|
||||
}
|
||||
out.close();
|
||||
ifstream in("/test.dat",ios::in|ios::binary);
|
||||
while(in.get(ch))
|
||||
cout<<ch;
|
||||
in.close();
|
||||
system("pause");
|
||||
}
|
||||
41
practical_exercises/10_day_practice/day10/文件例题/12-9.cpp
Normal file
41
practical_exercises/10_day_practice/day10/文件例题/12-9.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
//Eg12-12.cpp
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
using namespace std;
|
||||
class Employee{
|
||||
private:
|
||||
int number ,age;
|
||||
char name[20];
|
||||
double sal;
|
||||
public:
|
||||
Employee(){}
|
||||
Employee(int num,char* Name,int Age, double Salary){
|
||||
number=num;
|
||||
strcpy(name,Name);
|
||||
age=Age;
|
||||
sal=Salary;
|
||||
}
|
||||
void display(){
|
||||
cout<<number<<"\t"<<name<<"\t"<<age<<"\t"<<sal<<endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
ofstream out("D:/Employee.dat",ios::out); //定义随机输出文件
|
||||
Employee e1(1,"张三",23,2320);
|
||||
Employee e2(2,"李四",32,3210);
|
||||
Employee e3(3,"王五",34,2220);
|
||||
Employee e4(4,"刘六",27,1220);
|
||||
out.write((char*)&e1,sizeof(e1)); //按e1,e2,e3,e4顺序写入文件
|
||||
out.write((char*)&e2,sizeof(e2));
|
||||
out.write((char*)&e3,sizeof(e3));
|
||||
out.write((char*)&e4,sizeof(e4));
|
||||
|
||||
//下面的代码将e3(即王五)的年龄改为40岁
|
||||
Employee e5(3,"王五",40,2220);
|
||||
out.seekp(3*sizeof(e1)); //指针定位到第3(起始为0)个数据块
|
||||
out.write((char*)&e5,sizeof(e5)); //将e5写到第3个数据块位置,覆盖e3
|
||||
out.close(); //关闭文件
|
||||
system("pause");
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
//【例12-2】 用函数get和getline读取数据。
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
char a,b,c,d;
|
||||
cin.get(a);
|
||||
cin.get(b);
|
||||
c = cin.get();
|
||||
d = cin.get();
|
||||
cout<<int(a)<<','<<int(b)<<','<<int(c)<<','<<int(d)<<endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
用法:a = cin.get() ?或者 ?cin.get(a)
|
||||
结束条件:输入字符足够后回车
|
||||
说明:这个是单字符的输入,用途是输入一个字符,把它的ASCALL码存入到a中
|
||||
处理方法:与cin不同,cin.get()在缓冲区遇到[enter],[space],[tab]不会作为舍弃,而是继续留在缓冲区中
|
||||
*/
|
||||
@@ -0,0 +1,44 @@
|
||||
|
||||
//【例12-2】 用函数get和getline读取数据。
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
//cin.get(arrayname,size) 把字符输入到arrayname中,长度不超过size
|
||||
int main()
|
||||
{
|
||||
//get()两个参数
|
||||
|
||||
//1.输入串长<size,输入串长>arraylength,会自动扩张arrayname大小,使能保存所有数据
|
||||
// char a[10];
|
||||
// cin.get(a,20);
|
||||
// cout<<a<<endl;
|
||||
// cout<<sizeof(a)<<endl;
|
||||
//2.输入串长<size,输入串长<arraylength,把串全部输入,后面补‘\0’
|
||||
// char b[10];
|
||||
// cin.get(b,20);
|
||||
// cout<<b<<endl;//12345,此时数组内数据为‘12345'\0’
|
||||
// cout<<sizeof(b)<<endl;
|
||||
//3.输入串长>size,先截取size个字符,若还是大于arraylength,则输入前arraylength-1个字符,最后补充‘\0’
|
||||
// char c[5];
|
||||
// cin.get(c,10);
|
||||
// cout<<c<<endl;
|
||||
// cout<<sizeof(c)<<endl;
|
||||
//4.输入串长>size,先截取size个字符,若小于arraylength,则把截取串放入数组中,最后补充‘\0’
|
||||
// char d[10];
|
||||
// cin.get(d,5);
|
||||
// cout<<d<<endl;
|
||||
// cout<<sizeof(d)<<endl;
|
||||
|
||||
//get()三个参数
|
||||
/*
|
||||
用法:cin.get(arrayname,size,s) ?把数据输入到arrayname字符数组中,当到达长度size时结束或者遇到字符s时结束
|
||||
注释:a必须是字符数组,即char a[]l类型,不可为string类型;size为最大的输入长度;s为控制,遇到s则当前输入结束缓存区里的s将被舍弃
|
||||
|
||||
*/
|
||||
int i;
|
||||
char e[10];
|
||||
cin.get(e,8,',');
|
||||
cout<<e;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
/*
|
||||
(1)cin.getline(arrayname,size)与cin.get(arrayname,size)的区别
|
||||
cin.get(arrayname,size)当遇到[enter]时会结束目前输入,他不会删除缓冲区中的[enter]
|
||||
cin.getline(arrayname,size)当遇到[enter]时会结束当前输入,但是会删除缓冲区中的[enter]
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
/*
|
||||
char a[10];
|
||||
char b;
|
||||
cin.get(a,10);
|
||||
cin.get(b);
|
||||
cout<<a<<endl<<int(b);//输入:12345[enter] 输出:12345 【换行】 10*/
|
||||
/*char c[10];
|
||||
char d;
|
||||
cin.getline(c,10);
|
||||
cin.get(d);
|
||||
cout<<c<<endl<<int(d);//输入:12345[enter]a[enter] 输出:12345【换行】97*/
|
||||
//cin.getline(arrayname,size,s)与cin.gei(arrayname,size,s)的区别
|
||||
/*
|
||||
cin.getline(arrayname,size,s)当遇到s时会结束输入,并把s从缓冲区中删除
|
||||
cin.get(arrayname,size,s)当遇到s时会结束输入,但不会删除缓冲区中的s
|
||||
*/
|
||||
/*
|
||||
char e[10];
|
||||
char f;
|
||||
cin.get(e,10,',');
|
||||
cin.get(f);
|
||||
cout<<e<<endl<<f;//输入:12345,[enter] 输出:12345【换行】,说明:cin,get不会删除缓冲区的,*/
|
||||
char e1[10];
|
||||
char f1;
|
||||
cin.getline(e1,10,',');
|
||||
cin.get(f1);
|
||||
cout<<e1<<endl<<f1;//输入:asd,wqe 输出:asd【换行】w
|
||||
system("pause");
|
||||
}
|
||||
28
practical_exercises/10_day_practice/day10/文件例题/输出格式10-4.cpp
Normal file
28
practical_exercises/10_day_practice/day10/文件例题/输出格式10-4.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include<iostream>
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
char c[30]="this is string";
|
||||
double d = -1231.232;
|
||||
cout.width(30);
|
||||
cout.fill('*');
|
||||
cout.setf(ios::left);
|
||||
cout<<c<<"----L1"<<endl;
|
||||
cout.width(30);
|
||||
cout.fill('-');
|
||||
cout.setf(ios::right);
|
||||
cout<<c<<"----L2"<<endl;
|
||||
cout.setf(ios::dec|ios::showbase|ios::showpoint);
|
||||
cout.width(30);
|
||||
cout<<d<<"----L3"<<"\n";
|
||||
cout.setf(ios::showpoint);
|
||||
cout.precision(10);
|
||||
cout.width(30);
|
||||
cout<<d<<"----L4"<<"\n";
|
||||
cout.width(30);
|
||||
cout.setf(ios::oct,ios::basefield);
|
||||
cout<<100<<"----L5"<<"\n";
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
63
practical_exercises/10_day_practice/day10/文件例题/重要!!!课堂练习.cpp
Normal file
63
practical_exercises/10_day_practice/day10/文件例题/重要!!!课堂练习.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include<iostream>
|
||||
#include<fstream>
|
||||
//向量是一个能够存放任意类型的动态数组
|
||||
#include<vector>
|
||||
#include<cstring>
|
||||
using namespace std;
|
||||
class Person{
|
||||
private:
|
||||
char name[20];
|
||||
char id[18];
|
||||
int age;
|
||||
char addr[20];
|
||||
public:
|
||||
Person(){}
|
||||
Person(char *n,char *pid,int Age,char* Addr){
|
||||
strcpy(name,n);
|
||||
strcpy(id,pid);
|
||||
age=Age;
|
||||
strcpy(addr,Addr);
|
||||
}
|
||||
void display(){
|
||||
cout<<name<<"\t"<<id<<"\t"<<age<<"\t"<<addr<<endl;
|
||||
}
|
||||
};
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
vector<Person> v;
|
||||
vector<Person>::iterator pos;//声明一个迭代器,来访问vector容器,作用:遍历或者指向vector容器的元素
|
||||
char ch;
|
||||
ofstream out("d:/person.dat",ios::out|ios::app|ios::binary);
|
||||
char Name[20],ID[18],Addr[20];
|
||||
int Age;
|
||||
cout<<"------输入个人档案------"<<endl<<endl;
|
||||
do{
|
||||
cout<<"姓名: ";
|
||||
cin>>Name;
|
||||
cout<<"身份证号: ";
|
||||
cin>>ID;
|
||||
cout<<"年龄: ";
|
||||
cin>>Age;
|
||||
cout<<"地址: ";
|
||||
cin>>Addr;
|
||||
Person per(Name,ID,Age,Addr);
|
||||
out.write((char*)&per,sizeof(per));
|
||||
cout<<"Enter another Person(y/n)?";
|
||||
cin>>ch;
|
||||
}while(ch=='y');
|
||||
out.close();
|
||||
ifstream in("d:/person.dat",ios::in|ios::binary); //L9
|
||||
Person s;
|
||||
in.read((char*)&s,sizeof(s));
|
||||
while(!in.eof()){
|
||||
v.push_back(s);
|
||||
in.read((char*)&s,sizeof(s));
|
||||
}
|
||||
cout<<"\n---------从文件中读出的数据--------"<<endl<<endl;//L15
|
||||
pos=v.begin();
|
||||
for(pos=v.begin();pos!=v.end();pos++)
|
||||
(*pos).display();
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user