support bazel complie this project and format code.
This commit is contained in:
30
practical_exercises/10_day_practice/day10/file/10-4.cpp
Normal file
30
practical_exercises/10_day_practice/day10/file/10-4.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#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";
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,18 +1,17 @@
|
||||
//用cin输入字符串数据时,如果字符串中含有空白就不能完整输入。因为遇到空白字符时,cin就认为字符串结束了。
|
||||
#include<iostream>
|
||||
#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;
|
||||
int main(int argc, char const *argv[]) {
|
||||
char a[50];
|
||||
cout << "please input a string:";
|
||||
cin >> a;
|
||||
cout << a << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
/*
|
||||
若a的内容是:
|
||||
this is a string!
|
||||
this is a string!
|
||||
就难以输入啦!
|
||||
这样的数据应用输入流类的成员函数输入
|
||||
*/
|
||||
12
practical_exercises/10_day_practice/day10/file/12-2.cpp
Normal file
12
practical_exercises/10_day_practice/day10/file/12-2.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#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;
|
||||
|
||||
return 0;
|
||||
}
|
||||
19
practical_exercises/10_day_practice/day10/file/12-3.cpp
Normal file
19
practical_exercises/10_day_practice/day10/file/12-3.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
}
|
||||
20
practical_exercises/10_day_practice/day10/file/12-5.cpp
Normal file
20
practical_exercises/10_day_practice/day10/file/12-5.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Eg12-5.cpp
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
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";
|
||||
|
||||
}
|
||||
37
practical_exercises/10_day_practice/day10/file/12-6.cpp
Normal file
37
practical_exercises/10_day_practice/day10/file/12-6.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
// 12-6.cpp
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
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();
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
practical_exercises/10_day_practice/day10/file/12-7.cpp
Normal file
20
practical_exercises/10_day_practice/day10/file/12-7.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Eg12-7.cpp
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
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();
|
||||
|
||||
}
|
||||
45
practical_exercises/10_day_practice/day10/file/12-9.cpp
Normal file
45
practical_exercises/10_day_practice/day10/file/12-9.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
// Eg12-12.cpp
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
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(
|
||||
2 *
|
||||
sizeof(
|
||||
e1)); //指针定位到第3(起始为0)个数据块,这里写错了应该是2*sizeof(e1)
|
||||
out.write((char *)&e5, sizeof(e5)); //将e5写到第3个数据块位置,覆盖e3
|
||||
out.close(); //关闭文件
|
||||
|
||||
}
|
||||
48
practical_exercises/10_day_practice/day10/file/BUILD
Normal file
48
practical_exercises/10_day_practice/day10/file/BUILD
Normal file
@@ -0,0 +1,48 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-9`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-6`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:10-4`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-2`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:practice`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-1`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-5`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-3`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file:12-7`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "12-9",
|
||||
srcs = ["12-9.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-6",
|
||||
srcs = ["12-6.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "10-4",
|
||||
srcs = ["10-4.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-2",
|
||||
srcs = ["12-2.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "practice",
|
||||
srcs = ["practice.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-1",
|
||||
srcs = ["12-1.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-5",
|
||||
srcs = ["12-5.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-3",
|
||||
srcs = ["12-3.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "12-7",
|
||||
srcs = ["12-7.cpp"],
|
||||
)
|
||||
18
practical_exercises/10_day_practice/day10/file/input/BUILD
Normal file
18
practical_exercises/10_day_practice/day10/file/input/BUILD
Normal file
@@ -0,0 +1,18 @@
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file/input:getline`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file/input:get2`
|
||||
# please run `bazel run //practical_exercises/10_day_practice/day10/file/input:get`
|
||||
|
||||
load("@rules_cc//cc:defs.bzl", "cc_binary")
|
||||
|
||||
cc_binary(
|
||||
name = "getline",
|
||||
srcs = ["getline.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "get2",
|
||||
srcs = ["get2.cpp"],
|
||||
)
|
||||
cc_binary(
|
||||
name = "get",
|
||||
srcs = ["get.cpp"],
|
||||
)
|
||||
@@ -1,16 +1,15 @@
|
||||
//【例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;
|
||||
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;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -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,则自动扩张arrayname大小,使能保存所有数据
|
||||
// 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;
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
64
practical_exercises/10_day_practice/day10/file/practice.cpp
Normal file
64
practical_exercises/10_day_practice/day10/file/practice.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
//向量是一个能够存放任意类型的动态数组
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
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();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
#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");
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
//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");
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
//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;
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
//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");
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
//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(2*sizeof(e1)); //指针定位到第3(起始为0)个数据块,这里写错了应该是2*sizeof(e1)
|
||||
out.write((char*)&e5,sizeof(e5)); //将e5写到第3个数据块位置,覆盖e3
|
||||
out.close(); //关闭文件
|
||||
system("pause");
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
|
||||
//【例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,则自动扩张arrayname大小,使能保存所有数据
|
||||
// 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;
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
#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");
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
#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