The code looks like this:
Copy Code code as follows:
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace Std;
int _tmain (int argc, _tchar* argv[])
{
Write to File
Ofstream OFS; Provides the ability to write files
Ofs.open ("D:\\com.txt", Ios::trunc); Trunc Open the file, empty the existing file stream, and create the file if it does not exist
int i;
char a = ' a ';
for (i = 1; I!= ++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 read-file functionality
Ifs.open ("D:\\com.txt", ios::in);//in--open file for read operation
The contents of cout << "D:\\com.txt" << "are as follows:" << Endl;
while (!ifs.eof ())//Determine if the end of the stream is reached
{
Ifs.getline (buffer, 256, ' \ n '); The character reaches 256 or ends when it encounters a newline.
cout << buffer << Endl;
}
Ifs.close ();
}