C++读写初步

来源:互联网 发布:jquery怎么做分页java 编辑:程序博客网 时间:2024/06/08 18:14
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    /*ifstream in("E:\testfile\test.txt");//声明一个输入文件流 in,并默认调用.open()函数与文件关联
    //判断输入流是否与文件关联
    if (!in.is_open())
    {
        cout << "the file can't be opened" << endl;
        exit(1);//终止程序
    }
    while (!in.eof())//未到文件数据末尾时
    {
        char text[20];
        in.getline(text, 10);
        cout << text << endl;
    }
    in.close();*/
    //******************************
    ofstream out;//声明一个输出流 out
    out.open("E:\\testfile\\test.txt",ofstream::app);//与文件关联,第二参数注意
    //判断是否关联上了
    if (!out.is_open())
    {
        cout << "can't write into the file";
        exit(1);
    }
    out << "\nthe file can be writed.\n";
    out.close();
    return 0;
}
0 0
原创粉丝点击