C ++ basic learning tutorial (3)

Source: Internet
Author: User

Take the previous lecture.File I/O 2.7

C ++ has a special header file for reading and writing files. .

First, an example of reading a file is as follows:

 
 

/*************************************** * *********************************> File Name: list13020.file.cpp> Author: suool> Mail: 1020935219@qq.com> Created Time: thursday, May 22, 2014 ******************************** **************************************** /# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; int main () {ifstream in ("list1301.txt"); if (not in) {perror ("list1301.txt");} else {string x; while (in> x) {cout <x <endl;} in. close () ;}return 0 ;}
    
   
  
 

File Content:

Read operation result:

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + PC9wPgo8cD7Iu7rzysfQtM7EvP6jrMq + wP3I58/Co7o8L3A + cjxwp1_vcd4kphbyzsbjbgfzcz0 = "brush: java; "> /************************************* * ***********************************> File Name: list1302_write.cpp> Author: suool> Mail: 1020935219@qq.com> Created Time: Saturday May 24, 2014> Aim: copying Integers from a Named File to a Named File ****************************** **************************************** **/# include # Include # Include Using namespace std; int main () {// Read data from file in ifstream in ("data.txt"); if (not in) perror ("data.txt "); // the file does not exist. else {ofstream out ("out.txt"); // Write the data to out if (not out) perror ("out.txt "); // The file does not contain else {int x (0); while (in> x) out <x <'\ n'; out. close (); in. close (); // close the file stream} return 0 ;}

The file content is as follows:



Running result:


However, the above read and write files have some problems, that is, the program does not check whether the output operation is successfully executed. The following program transformation is an example with a minimum error check:

/*************************************** * *********************************> File Name: list1302_write_check.cpp> Author: suool> Mail: 1020935219@qq.com> Created Time: ******************************** **************************************** /# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
Using namespace std; int main () {// read data from in ifstream in ("data.txt"); if (not in) perror ("data.txt "); else {ofstream out ("out.txt"); if (not out) perror ("out.txt"); else {int x (0); while (in> x) out <x <endl; out. close (); if (not out) {perror ("ou.txt") ;}} return 0 ;}
    
   
  
 

Is this part:


2.8 Data Structure- ing

We have already introduced and learned a unique data structure-vector of C ++. Now I want to introduce another special data structure- ing of C ++, other advanced languages are called dictionaries. In fact, they are all the same as key-value pairs. The key is unique and the value is not limited.

The following is an example:

/*************************************** * *********************************> File Name: list1401_data.cpp> Author: suool> Mail: 1020935219@qq.com> Created Time: ******************************** **************************************** /// read the word and count the number of times it appears # include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       Using namespace std; int main () {map
        
          Counts; string word; fstream in ("data.txt"); if (not in) {perror ("data.txt ");} // Read words from the standard input and count the number of times // each word occurs. cout <"Read words from data.txt, spreate by blank space" <endl; // For each word/count pair... ofstream out ("out.txt"); if (not out) {perror ("out.txt");} while (in> word) {++ counts [word]; out <word <endl;} // out. close (); in. close (); cout <"The words and count are:" <endl; out <"The words and count are:" <endl; for (map
         
           : Iterator iter (counts. begin (); iter! = Counts. end (); ++ iter) {// Print the word, tab, the count, newline. cout <iter-> first <'\ t' <iter-> second <' \ n '; out <iter-> first <'\ t' <iter-> second <' \ n';} out. close (); if (not out) {perror ("out.txt") ;}return 0 ;}
         
        
      
     
    
   
  
 

The result is as follows:



The following example uses the iterator to cyclically format the output ing content:

/*************************************** * *********************************> File Name: list1401_data_compat.cpp> Author: suool> Mail: 1020935219@qq.com> Created Time: ******************************** **************************************** /# include
 
  
# Include
  
   
# Include
   
    
# Include
    # Include
     
      
# Include
      
        // Aligning Words and Counts Neatlyusing namespace std; int main () {map
       
         Counts; string word; // read data from data.txt fstream in ("data.txt"); if (not in) {perror ("data.txt ");} cout <"Read words from data.txt, spreate by blank space" <endl; ofstream out ("out.txt"); if (not out) {perror ("out.txt ");} // write data to out.txt out <"Begin !!!! Hahahahahahahhaha "<endl; while (in> word) {++ counts [word]; out <word <endl ;}// Determine the longest word. string: size_type longest (0); for (map
        
          : Iterator iter (counts. begin (); iter! = Counts. end (); ++ iter) if (iter-> first. size ()> longest) longest = iter-> first. size (); // For each word/count pair... const int count_size (10); // Number of places for printing the count out <"hahahahahahhaha !!!! "<Endl; for (map
         
           : Iterator iter (counts. begin (); iter! = Counts. end (); ++ iter) {// Print the word, count, newline. keep the columns neatly aligned. cout <setw (longest) <left <iter-> first <setw (count_size) <right <iter-> second <'\ n '; out <setw (longest) <left <iter-> first <setw (count_size) <right <iter-> second <'\ n ';} return 0 ;}
         
        
       
      
     
   
  
 

File Content:


Running result:



The following example shows the specified key in the search ing:

/*************************************** * *********************************> File Name: list1401_data_serach.cpp> Author: suool> Mail: 1020935219@qq.com> Created Time: ******************************** **************************************** /# include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      Using namespace std; int main () {map
       
         Counts; string word; // read data from data.txt ifstream in ("word.txt"); if (not in) {perror ("word.txt ");} // write data to out.txt ofstream out ("out.txt"); if (not out) {perror ("out.txt") ;}out <"Begin !!!! Hahahhahah !!! \ N "; while (in> word) {++ counts [word]; out <word <'\ n';} out. close (); map
        
          : Iterator the (counts. find ("the"); if (the = counts. end () cout <"\" the \ "is not found !!!! "<Endl; else if (the-> second = 1) cout <"\" the \ "occurs" <the-> second <"time \ n "; else cout <"\" the \ "occurs" <the-> second <"times \ n"; return 0 ;}
        
       
     
    
   
  
 

File Content:

Running result:




Not complete .........


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.