Perform read and write operations on files in C ++

Source: Internet
Author: User

# Include "stdafx. h"

# Include <fstream>
# Include <iostream>
# Include <cmath>

Using namespace std;

// Function for reading characters from the keyboard
Void read_save (){
Char c [80];
Ofstream outfile ("f1.dat"); // open the file with an outputer
If (! Outfile ){
Cerr <"open error! "<Endl; // note that cerr is used
Exit (1 );
}
Cin. getline (c, 80); // read a line of characters from the keyboard
For (int I = 0; c [I]! = 0; I ++) // processes the characters one by one until/0 is met
If (c [I]> = 65 & c [I] <= 90 | c [I]> = 97 & c [I] <= 122) {// ensure that the entered character is a character
Outfile. put (c [I]); // saves letters to Disk Files
Cout <c [I] <"";
}
Cout <endl;
Outfile. close ();
}
Void creat_data (){
Char ch;
Ifstream infile ("f1.dat", ios: in); // open the file as input
If (! Infile ){
Cerr <"open error! "<Endl;
Exit (1 );
}
Ofstream outfile ("f3.dat"); // defines the output stream f3.dat File
If (! Outfile ){
Cerr <"open error! "<Endl;
Exit (1 );
}
While (infile. get (ch) {// when the character is successfully read
If (ch <= 122 & ch> = 97)
Ch = ch-32;
Outfile. put (ch );
Cout <ch;
}
Cout <endl;
Infile. close ();
Outfile. close ();
}

Int main (){
Read_save ();
Creat_data ();
System ("pause ");
Return 0;
}


# Include "stdafx. h"

# Include <fstream>
# Include <iostream>
# Include <cmath>

Void read_save ();
Void creat_data ();

Using namespace std;

Int main (){
Read_save ();
Creat_data ();
System ("pause ");
Return 0;
}

// Function for reading characters from the keyboard
Void read_save (){
Char c [80];
Ofstream outfile ("f1.dat"); // open the file with an outputer
If (! Outfile ){
Cerr <"open error! "<Endl; // note that cerr is used
Exit (1 );
}
Cin. getline (c, 80); // read a line of characters from the keyboard
For (int I = 0; c [I]! = 0; I ++) // processes the characters one by one until/0 is met
If (c [I]> = 65 & c [I] <= 90 | c [I]> = 97 & c [I] <= 122) {// ensure that the entered character is a character
Outfile. put (c [I]); // saves letters to Disk Files
Cout <c [I] <"";
}
Cout <endl;
Outfile. close ();
}
Void creat_data (){
Char ch;
Ifstream infile ("f1.dat", ios: in); // open the file as input
If (! Infile ){
Cerr <"open error! "<Endl;
Exit (1 );
}
Ofstream outfile ("f3.dat"); // defines the output stream f3.dat File
If (! Outfile ){
Cerr <"open error! "<Endl;
Exit (1 );
}
While (infile. get (ch) {// when the character is successfully read
If (ch <= 122 & ch> = 97)
Ch = ch-32;
Outfile. put (ch );
Cout <ch;
}
Cout <endl;
Infile. close ();
Outfile. close ();
}


C: The f1.dat and f3.dat files are generated under the Symbian8.0aS60 _ 2nd_FP2_SCProjectsTestArray project directory, and the files contain characters entered from the console each time the program runs.
 

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.