11-7
#includeusing 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);//设定输出值以新的浮点数精度值显示,即小数点后保留2位 cout.setf(ios_base::uppercase|ios_base::scientific);//以科学格式显示浮点数值,对于科学格式显示大写字母E cout<<831.0; cout.flags(original_flags);//恢复初始的的格式化参数 return 0;}
11-3
#include#include #include using namespace std;int main(){ ofstream outFile; outFile.open(("D:\\text1.txt")); outFile<<"已成功写入文件"<
11-4
#include#include #include using namespace std;int main(){ ifstream outFile("D:\\text1.txt"); string file; if(outFile) { while(getline(outFile,file)) { cout< <
1
#include#include #include #include #include using namespace std;struct student { string num1; string num2; string name; string c; };int main(){ student stu[100]; int i=0; ifstream inFile("list.txt"); if(!inFile) { cout<<"无法打开文件"< >stu[i].num1>>stu[i].num2>>stu[i].name>>stu[i].c) { i++; } inFile.close(); ofstream outFile("roll.txt"); srand((unsigned)time(NULL)); for(int j=0;j<5;j++) { int x; x=rand()%84; cout< <<" "< <<" "< <<" "< <
2
#include#include #include #include #include #include using namespace std;int main(){ ifstream outFile("text2.txt"); int zf=0,word=0,line=0; char a[1000]; while(outFile.getline(a,1000)) { for(int i=0;i ='A'&&a[i]<='Z'||a[i]>='a'&&a[i]<='z') { if(a[i-1]>='A'&&a[i-1]<='Z'||a[i-1]>='a'&&a[i-1]<='z') { if(a[i+1]=='.'||a[i+1]==','||a[i+1]==' '||a[i+1]=='?'||a[i+1]=='!') word++; } if(a[i-1]>=' ') { if(a[i+1]=='.'||a[i+1]==','||a[i+1]=='?'||a[i+1]=='!') word++; } } } line++; } cout<<"字符:"< < <<"单词:"< < <<"行:"<
总结:
这次实验上我对对文件的I/O控制方法有了更深的了解,更熟练的掌握了了打开关闭文件,以及向文件内输入的方法,与前几次实验相比比较简单