This commit is contained in:
Light-City
2020-04-06 00:57:02 +08:00
parent f97c156cc4
commit a4d828bb4c
120 changed files with 4413 additions and 0 deletions

View File

@@ -0,0 +1 @@
# 文件与流

View 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!
就难以输入啦!
这样的数据应用输入流类的成员函数输入
*/

View 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;
}

View 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");
}

View 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");
}

View 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;
}

View 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");
}

View 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");
}

View File

@@ -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]不会作为舍弃,而是继续留在缓冲区中
*/

View File

@@ -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;
}

View File

@@ -0,0 +1,38 @@
#include<iostream>
using namespace std;
/*
1cin.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.getarrayname,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");
}

View 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;
}

View 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;
}