标签:txt 英文 image names open srand strftime div --
#include <iostream> #include <fstream> #include <string> #include <cstdlib> using namespace std; int main() { char filename1[10], filename2[10], newfilename[10]; cout << "输入要合并的两个文件名: " ; gets(filename1); gets(filename2); cout << "输入合并后新文件名: " ; cin >> newfilename; ofstream fout; ifstream fin; fin.open(filename1); if(!fin.is_open()) { cerr << "fail to open file " << filename1 << endl; system("pause"); exit(0); } fout.open(newfilename); if(!fin.is_open()) { cerr << "fail to open file " << newfilename << endl; system("pause"); exit(0); } char ch; while(fin.get(ch)) fout << ch; fin.close(); fout << endl; fin.open(filename2); if(!fin.is_open()) { cerr << "fail to open file " << filename2 << endl; system("pause"); exit(0); } while(fin.get(ch)) fout << ch; fin.close(); fout<<"\nmerge successfully."<<endl; fout.close(); system("pause"); return 0; }
#include<fstream> #include <string> #include <cstdlib> #include<sstream> #include<ctime> #include "utils.h" using namespace std; int main() { ifstream fin; ofstream fout; string filename1; int num, totalline = 0; cout << "输入名单列表文件名:"; cin >> filename1; cout << "输入随机抽点人数:"; cin >> num; fin.open(filename1, ios_base::in); if (!fin.is_open()) { cerr << "fail to open file " << filename1 << endl; system("pause"); exit(0); } string temp; string a[100]; if (fin) { while (getline(fin, temp)) { a[totalline++] = temp; } fin.close(); } string filename; filename = getCurrentDate()+".txt"; cout << "new file name is "<<filename << endl; srand(std::time(0)); for (int i = 0;i < num;i++) { int line; int random = rand(); line = rand()%totalline ; cout << a[line] << endl; fout.open(filename, ios_base::app); fout << a[line] << endl; fout.close(); } system("pause"); return 0; } utils.h #include <string> using std::string; string getCurrentDate(); utils.cpp #include "utils.h" #include <ctime> using std::string; const int SIZE = 20; string getCurrentDate() { time_t time_seconds = time(0); struct tm now_time; localtime_s(&now_time, &time_seconds); char date[SIZE]; strftime(date, SIZE, "%Y%m%d", &now_time); return (string(date))
p4
#include<iostream> #include<fstream> #include<string> using namespace std; int main() { string fn,temp; char tch; ifstream fin; int charcnt,wordcnt,linecnt; cout<<"输入要统计的英文文本文件名:"; cin>>fn; fin.open(fn); if(!fin.is_open()) { cerr << "fail to open file"<<fn<< endl; system("pause"); exit(0); } charcnt = 0; while(fin.get(tch)) charcnt++; charcnt--; wordcnt=0; fin.close(); fin.open(fn); while(fin>>temp) wordcnt++; linecnt=0; fin.close(); fin.open(fn) ; while(getline(fin,temp)) linecnt++; fin.close(); cout<<"字符数:"<<charcnt<<endl; cout<<"单词数:"<<wordcnt<<endl; cout<<"行数:"<<linecnt<<endl; system("pause"); return 0; }
标签:txt 英文 image names open srand strftime div --
原文地址:https://www.cnblogs.com/sora5934/p/11044594.html