//11-7
#include<iostream>
using namespace std;
int main()
{
ios_base::fmtflags original_flags=cout.flags();
// flag指针?指向开头?
cout<<812<<'|';
cout.setf(ios_base::left,ios_base::adjustfield);
// 输出左对齐
cout.width(10);
// 输出长度位10
cout<<813<<815<<'\n';
cout.unsetf(ios_base::adjustfield);
// 调整,取消左对齐
cout.precision(2);
cout.setf(ios_base::uppercase|ios_base::scientific);
// 科学计数法表示,并且小数点后保留两位有效数字
cout<<831.0;
cout.flags(original_flags);
return 0;
}
//11-8
#include<fstream>
#include<iostream>
#include<string>
using namespace std;
int main()
{
ofstream fout("text1.txt");
if(!fout){
cout << "fail to build" << endl;
return 0;
}
fout << "已成功写入文件!";
fout.close();
return 0;
}
//11-9
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
string s;
ifstream fin("text1.txt");
if(!fin){
cout << "fail to open" << endl;
return 0;
}
fin >> s;
cout << s <<endl;
fin.close();
return 0;
}
//应用练习1
#include<iostream>
#include<fstream>
#include<vector>
#include<ctime>
#include<string>
using namespace std;
struct stu
{
string i1;
string i2;
string i3;
string i4;
};
int main()
{
ifstream fin("list.txt");
stu k;
vector<stu> tar;
if(!fin){
cout << "fail to open"<<endl;
return 0;
}
string i1,i2,i3,i4;
while(fin >> i1 >> i2 >> i3 >> i4){
k.i1=i1,k.i2=i2,k.i3=i3,k.i4=i4;
tar.push_back(k);
}
fin.close();
vector<stu> d;
srand((unsigned)time(NULL));
while(d.size()<5){
d.push_back(tar[rand()%83]);
}
ofstream fout("roll.txt");
for(int i=0;i<d.size();i++){
fout << d[i].i1 << " " << d[i].i2 << " ";
fout << d[i].i3 << " " << d[i].i4 << endl;
}
fout.close();
return 0;
}
//应用练习2
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
ifstream fin1("num1.txt");
char ch,prech='\n';
int line=0,word=0,words=0;
while((ch=fin1.get())!=EOF){
if(ch=='\n')line++;
if(ch!=' '&&ch!='\n')word++;
if((prech==' '&&ch!=' ')||(prech=='\n'&&ch!=' '))words++;
prech=ch;
}
cout << "num.txt has "<< word << " word" << endl;
cout << "num.txt has "<< words << " words" << endl;
cout << "num.txt has "<< line << " line" << endl;
return 0;
}