How to Implement C ++ file stream operations

Source: Internet
Author: User

In actual programming, C ++ is a simple operation for file operations. You can use some simple examples to fully master this application skill, it also helps your program development in actual programming. Next let's take a look at the application skills related to file stream operations in C ++.

File writing for C ++ file stream operations:

 
 
  1. # Include <fstream>
  2. # Include <iostream>
  3. Using namespace std;
  4. Int main (){
  5. String str;
  6. Ofstream out ("d.txt ");
  7. Str = "Moonlight in front of bed \ n suspected to be frost on the ground \ n raised his head to the moon \ n bowed his head to his hometown \ n ";
  8. Out <str <endl;
  9. Return 0;
  10. }

C ++ file stream operations for file reading:

 
 
  1. #include < fstream> 
  2. #include < iostream>   
  3. using namespace std;  
  4. int main(){  
  5. ifstream in("a.txt");  
  6. for(string str;getline(in,str);)  
  7. cout< < str< < "\n";  
  8. return 0;   

File Replication in C ++ file stream operations

 
 
  1. # Include <fstream>
  2. # Include <iostream>
  3. Using namespace std;
  4. Int main (){
  5. Ifstream in ("a.txt ");
  6. Ofstream out ("B .txt ");
  7. For (string str; getline (in, str );)
  8. Out <str <endl;
  9. Cout <"file copied !!! ";
  10. Return 0;
  11. }

Screening Method for Determining prime numbers

 
 
  1. #include < iostream> 
  2. #include < vector> 
  3. #include < fstream> 
  4. using namespace std;  
  5. int main(){  
  6. vector< int> prime(10000,1);  
  7. for(int i=2;i< 100;++i)  
  8. if(prime[i])  
  9. for(int j=i;i*j< 10000;++j)  
  10. prime[i*j]=0;  
  11. ifstream in("c.txt");  
  12. for(int a;in>>a && a>1 && a< 10000;)  
  13. cout< < a< < " is "< < (prime[a]?"":"not ")< < " a prime.\n";   

In the c.txt file, write a series of integers smaller than 10000 and wrap the line to determine the prime number, because it also involves the reading operation of the file, so it is also given.

I feel that the C ++ file stream is operating esay! It is more complicated to read files from Java.

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.