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:
- # Include <fstream>
- # Include <iostream>
- Using namespace std;
- Int main (){
- String str;
- Ofstream out ("d.txt ");
- 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 ";
- Out <str <endl;
- Return 0;
- }
C ++ file stream operations for file reading:
- #include < fstream>
- #include < iostream>
- using namespace std;
- int main(){
- ifstream in("a.txt");
- for(string str;getline(in,str);)
- cout< < str< < "\n";
- return 0;
- }
File Replication in C ++ file stream operations
- # Include <fstream>
- # Include <iostream>
- Using namespace std;
- Int main (){
- Ifstream in ("a.txt ");
- Ofstream out ("B .txt ");
- For (string str; getline (in, str );)
- Out <str <endl;
- Cout <"file copied !!! ";
- Return 0;
- }
Screening Method for Determining prime numbers
- #include < iostream>
- #include < vector>
- #include < fstream>
- using namespace std;
- int main(){
- vector< int> prime(10000,1);
- for(int i=2;i< 100;++i)
- if(prime[i])
- for(int j=i;i*j< 10000;++j)
- prime[i*j]=0;
- ifstream in("c.txt");
- for(int a;in>>a && a>1 && a< 10000;)
- 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.