码迷,mamicode.com
首页 > 其他好文 > 详细

实验6

时间:2019-06-18 14:32:48      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:txt   英文   image   names   open   srand   strftime   div   --   

 p2
#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;
}

技术图片

 

p3
#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; 
     
}

技术图片

 

 

实验6

标签:txt   英文   image   names   open   srand   strftime   div   --   

原文地址:https://www.cnblogs.com/sora5934/p/11044594.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!