C++文件的写入和读取

来源:互联网 发布:香水小样 知乎 编辑:程序博客网 时间:2024/06/09 19:20

//文件保存自用

// Filestream.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{ //打开文件 写
ofstream ofs("a.txt", ios::out);
ofs << "this is out!" << endl;
//关闭文件
ofs.close();
//打开文件 读
ifstream off("a.txt", ios::in);
char buf[80];
//是否到文件尾
while (!off.eof()) {
off.getline(buf, 80);
cout << buf << endl;}
//关闭文件
off.close();
    return 0;
}