实验六
验证实验一

part2 基础训练
#include "pch.h" #include <iostream> #include<fstream> using namespace std; int main() { ifstream fin; ofstream fout; fout.open("3.txt", ios::app); fout << "mergy sucessfully" << endl; fin.close(); fout.close(); return 0; }

part3应用编程实践
1.随机点名
#include <iostream> #include <string> #include <cstdlib> #include<fstream> #include<ctime> #include "utils.h" char s[83][80]; using namespace std; int main() { int n, i, j, t; string fname; ifstream fin; ofstream fout; char c; srand((unsigned)time(NULL)); cout << "输入名单列表文件名:"; cin >> fname; cout << "输入随机抽点人数:"; cin >> n; fin.open(fname); for (i = 0; i < 83; i++) { for (j = 0; j < 80; j++) { c = fin.get(); s[i][j] = c; if (c == '\n') break; } } fin.close(); fout.open(getCurrentDate()); for (i = 0; i < n; i++) { t= rand() % 83+1; cout << s[t]; fout<< s[t]; } fout.close(); system("pause"); return 0; }


2.统计
#include "pch.h" #include<iostream> #include<fstream> #include<string> using namespace std; int main() { char fname[100]; cout << "输入要统计的英文文件名:" << endl; cin.getline(fname, 100); fstream fout(fname, ios::in); fstream fin(fname); char ch; int c= 0; int i = 0; string d; while (fin && fin.get(ch)) { c++; } fin.close(); cout << "字符数: " << c-2<< endl; int word = 0; char str1[100]; ifstream in(fname); while (!in.eof()) { in >> str1; word++; } cout << "单词数:" << word << endl; in.close(); ifstream infile(fname); int n = 0; while (!infile.eof()) { infile.getline(fname, sizeof(fname));//默认终止标识符为‘\n’ n++; } cout << "行数:" << n << endl; fout.close(); system("pause"); return 0; }

1.统计题里的while(!in.eof())是确保取到文件里的每一个数据,当全部取完的时候退出while循环。
2.统计这一题里一开始得到字符数是67,发现把两个换行符也包括了,输出的时候要再减二。
3.part2基础训练我是又把3.txt复制到一个新项目的文件夹下,然后开始做题的。
4.part1验证实验刚做的时候发现明明已经把两个txt文件复制到项目文件架下了,却还是打不开,原来是要把文本文档复制到项目文件里那个与项目同名的文件下才可以,

浙公网安备 33010602011771号