标签:src 写入文件 base public original TE code return getline
11-7
#include<iostream> using namespace std; int main(){ ios_base::fmtflags original_flags=cout.flags();//保存目前设置 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-3
#include<iostream> #include<fstream> using namespace std; int main(){ ofstream file("test1.txt"); file<<"已成功写入文件!"<<endl; file.close(); return 0; }
文件截图
11-4
#include<iostream> #include<fstream> using namespace std; int main(){ ifstream file; file.open("test1.txt"); if(!file){cout<<"fail to open"<<endl;} char ch; while((ch=file.get())!=EOF){cout.put(ch);} file.close(); if(cin>>ch); return 0; }
运行截图
2.1 代码
#include<iostream> #include<fstream> #include<ctime> #include<cstdlib> #include<string> using namespace std; class Dice { public: Dice(int a) {sides = a;} int cast() { int b; b = rand() % sides + 1; return b;} private: int sides;}; int main() { Dice random(83); srand(time(NULL)); ifstream list; list.open("list.txt"); if(!list) cout<<"fail to open"<<endl; string info[83],str; int i=0,stu; while(getline(list,str)){ info[i]=str; i++; } list.close(); ofstream roll; roll.open("roll.txt"); for(i=0;i<5;i++){ stu=random.cast(); cout<<info[stu-1]<<endl; roll<<info[stu-1]<<endl; } roll.close(); return 0; }
运行截图
标签:src 写入文件 base public original TE code return getline
原文地址:https://www.cnblogs.com/krousar/p/9206159.html