Experimental libraries stream 6 and I / O

Part1 validation experiments

1. Merge the two files into a new file. File names are entered from the keyboard.

// merge two file contents to a new file.
// file name are entered from the keyboard 

#include <the iostream> 
#include <the fstream>    
#include < String > 
#include <the cstdlib>
 the using  namespace STD; 

int main () {
     String filename1, filename2, newfilename; 
    
    COUT << " Enter merge two file names: " ; 
    cin >> filename1 >> filename2; 
    cout << " enter the new merged file name: " ; 
    cin >> newfilename;
    // output file stream object 
    the ifstream fin;         // input file stream object 
    
    
    fin.open (filename1);   // input stream object file and the file filename1 associated fin 
    IF {(fin.is_open ()!)   // if the file is opened On failure, the error message and exit 
        cerr << " fail to Open file " << filename1 << endl; 
        System ( " PAUSE " ); 
        exit ( 0 );     
    } 
    
    fout.open (newfilename);     // output file fout stream object and file newfilename associate 
    IF (! fin.is_open ()) {   // if the create / open file failed, output an error message and exit   
        cerr <<" Fail to Open File " << newfilename << endl; 
        System ( " PAUSE " ); 
        Exit ( 0 ); 
    } 
    
    char CH; 
    
    // Stream object acquired fin characters from the input file and insert it into the file output stream object fout of 
    the while (. fin GET (CH)) 
        fout << CH; 
    
    fin.close ();   // close file associated with the input stream object file filename1 the fin 
    
    fout << endl; // inserted into the file output stream object fout wrap 
    
    
    fin.open (filename2);   // input stream object file and the file filename2 fin associated 
    IF (! fin.is_open ()) {   //If you open the file fails, output an error message and exit 
        cerr << " Fail to Open File " << filename2 << endl; 
        System ( " PAUSE " ); 
        Exit ( 0 );     
    } 
    
    // from the file input stream object fin Get a character, and inserted into the stream object file output fout of 
    the while (fin. GET (CH)) 
        fout << CH; 
    
    fin.close ();      // close file associated with the input stream object file filename2 the fin 
    fout.close ();     // close file output stream associated with the object is fout file newfilename 

    System ( " PAUSE " ); 
    
    return  0  ;
}
EX1

 

Part2 basic exercises
using file I / O stream to open the text file in Part1 after the merger, the last line in the file add the character "merge successfully."

#include <iostream>
#include <fstream>   
#include <string>
#include <cstdlib>
using namespace std;

int main() {
    string filename;
    cout << "打开文件: " ;
    cin >> filename;
    ofstream fout(filename,ios_base::app);        
    if(!fout.is_open()) { 
        cerr << "fail to open file " << filename << endl;
        system("pause");
        exit(0);    
    }
    fout<<endl<<"merge successfully."<<endl;
    fout.close(); 
    return 0;
} 
ex2

 

Part3 Application Programming Practices
1. List the list of known file list.txt. Write an application to achieve an n-bit random snapshot students (n input from the keyboard) from the list, displays the results on the screen, and also writes the results to a text file, the file name system automatically reads the date of the day, such as 20190611.txt .

#include <iostream>
#include<fstream>
#include <string>
#include <cstdlib>
#include<sstream>
#include<ctime>
#include "utils.h"

using namespace std;

int main() {
    ifstream fin;
    ofstream fout;
    string filename;
    int n, totalline = 0;
    cout << "输入名单列表文件名:";
    cin >> filename;
    cout << "输入随机抽点人数:";
    cin >> n;
    fin.open(filename, ios_base::in);
    if (!fin.is_open()) {
        cerr << "fail to open file " << filename << endl;
        system("pause");
        exit(0);
    }
    string s, a[100];
    if (fin) {
        while (getline(fin, s)) {
            a[totalline++] = s;
        }
        fin.close();
    }
    string filename1;
    filename = getCurrentDate() + ".txt";
    cout << "随机抽点中..." << filename1<< endl;
    srand(std::time(0));
    for (int i=1;i<=n;i++) {
        int num;
        int rand0 = rand();
        num= rand() % totalline;
        cout <<  a[num] << endl;
        fout.open(filename, ios_base::app);
        fout << a[num] << endl;
        fout.close();
    }
    system("pause");
    return 0;
}
main
#include <string>
using std::string;
string getCurrentDate();
utils.h
#include " utils.h "  
#include <the ctime> 
 the using STD :: string ;
 const  int SIZE = 20 is ;
 // Functions Function Description: Returns the current system time 
 @ parameters: no parameter
 // Return Value Description: A string type returns the current system date format such as 20,190,611 
String getCurrentDate () {         
    time_t time_seconds = Time ( 0 );    
     struct (TM) now_time;     
    the localtime_s ( & now_time, & time_seconds); // use safer the localtime_s ()        
    char dATE [SIZE]; 
    the strftime ( DATE, SIZE, "%Y%m%d", &now_time);        
    return (string(date));
}
utils.cpp

2. The number of characters in the English program statistics text file (including spaces), words, lines. Enter the file name from the keyboard. 

#include <iostream>
#include <fstream>   
#include <string>
using namespace std;
int main() {
    string filename;      
    cout<<"输入要统计的文本文件名:" ;
    cin>>filename;
    ifstream fin;
    ofstream out;
    fin.open(filename);
    if(!fin.is_open()) {
        cerr << "fail to open file "<<endl;
        system("pause");
        exit(0);    
    }
    char ch;
    int c=0,w=0,l=1;
    while(fin.get(ch)){
        if(ch==' '){
            w++;
            c++;
        }
        else if(ch!=' '&&ch!='\n'){
            c++;
        }
        else{
            l++;
        }
    }
        cout<<"字符数:"<<c<<endl;
        cout<<"单词数:"<<w+l<<endl;
        cout<<"行数:"<<l<<endl;         
        fin.close();
 } 
main

实验总结:这次实验做了很久,尤其是part 3的部分,我简直是无从下手,真的是很头痛。part 3的第一题我一开始截图就截少了,又重新运行,结果发现程序有错的地方,又花了好长时间才完成。通过这次实验,发现自己还是会有很多问题,还是要花很多时间,效率不高。

    

 

Guess you like

Origin www.cnblogs.com/wjh1022/p/11028328.html