Implementation of simple reading and writing of text files in C ++
Source: Internet
Author: User
The Code is as follows:
Copy codeThe Code is as follows: # include "stdafx. h"
# Include <iostream>
# Include <fstream>
Using namespace std;
Int _ tmain (int argc, _ TCHAR * argv [])
{
// Write a file
Ofstream ofs; // provides the file Writing Function
Ofs. open ("d: \ com.txt", ios: trunc); // trunc clears the existing file stream when opening the file. If the file does not exist, create
Int I;
Char a = 'a ';
For (I = 1; I! = 27; ++ I)
{
If (I <10)
{
Ofs <"0" <I <"\ t" <a <"\ n ";
A ++;
}
Else
{
Ofs <I <"\ t" <a <"\ n ";
A ++;
}
}
Ofs. close ();
// Read the file to the console
Char buffer [256];
Ifstream ifs; // provides the File Reading Function
Ifs. open ("d: \ com.txt", ios: in); // in -- open the file for read Operations
The content in cout <"d :\\ com.txt" <"is as follows:" <endl;
While (! Ifs. eof () // determines whether the stream ends.
{
Ifs. getline (buffer, 256, '\ n'); // The number of characters reaches 256 or ends when a line break occurs.
Cout <buffer <endl;
}
Ifs. close ();
}
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