C++实现对简单的文件读写

来源:互联网 发布:免费海关数据 编辑:程序博客网 时间:2024/05/13 17:56

#include<iostream>

#include<fstream>

                                                                              using namespace std;

                                                                             #define MAX_BUFFER_SIZE 1024
                                                                              int main()

                                                                             {

                                                                                     ifstream fin;//用于对文件的读操作

                                                                                     ofstream fout;//用于对文件的写操作

                                                                                     fin.open("c++_test.txt",ios::in); //打开文件,可读

                                                                                     fout.open("c++_test.txt",ios::out|ios::app);//打开文件可写可追加

                                                                                     char buffer[MAX_BUFFER_SIZE];


                                                                                      fin>>buffer;//读取文件内容

                                                                                      cout<<"read:>"<<buffer<<endl;


                                                                                      cin>>buffer;//输入要写入文件的内容

                                                                                      fout<<buffer;//将输入内容写入文件

                                                                                      cout<<"write:>"<<buffer<<endl;


                                                                                      return 0;

                                                                              }