C++对文件的操作

1. 文件写入:

#include <fstream>
using namespace std;

void main()
{
    ofstream out;    //创建一个文件输出流
    out.open("C:\\123.txt");    //输出到文件
    out << "锄禾日当午,汗滴禾下土。" << endl;
    out.close();


    system("C:\\123.txt");
}

2. 文件读取:

#include <fstream>
#include <iostream>
using namespace std;

void main()
{
    ifstream in;    //创建一个文件输出流
    in.open("C:\\123.txt");    //从文件读取

    char str[256]{ 0 };
    //in >> str;
    in.getline(str, 256);//处理空格

    in.close();

    cout << str << endl;

    cin.get();
}

3. 文件追加:

#include <fstream>
using namespace std;

void main()
{
    ofstream out;    //创建一个文件输出流
    out.open("C:\\123.txt",ios::app);    //追加方式写入文件
    out << "谁知盘中餐,粒粒皆辛苦。" << endl;
    out.close();


    system("C:\\123.txt");
}

    

4. 文本文件读写:

#include <iostream>
#include <fstream>

using namespace std;

struct info
{
    char name[10];
    int id;
    double price;
};

void main()
{
    struct info infs[3] = { {"xiaohua",99,5000},{"xiaohong",89,4000},{"xiaoli",79,3000} };
    //ofstream fout("C:\\3-文件操作练习\\1.txt",ios::out|ios::app);
    ofstream fout("C:\\3-文件操作练习\\1.txt");
    for (auto i : infs)
    {
        fout << i.name << " " << i.price << " " << i.id << endl;
    }
    fout.close();

    ifstream fin("C:\\3-文件操作练习\\1.txt");
    for (int i = 0; i < 3; i++)
    {
        char str[255]{ 0 };
        fin.getline(str, 254);
        cout << str << endl;
    }
    fin.close();

    cin.get();
}

5. 二进制文件读写:

#include <iostream>
#include <fstream>

using namespace std;

struct info
{
    char name[10];
    int id;
    double price;
};

void main()
{
    struct info infs[3] = { { "xiaohua",99,5000 },{ "xiaohong",89,4000 },{ "xiaoli",79,3000 } };
    ofstream fout("C:\\3-文件操作练习\\2.bin",ios::binary);
    fout.write((char *)infs, sizeof(infs));    //从内存写入磁盘
    fout.close();

    struct info infshua[3]{ 0 };
    ifstream fin("C:\\3-文件操作练习\\2.bin", ios::binary);
    fin.read((char *)infshua, sizeof(infshua));
    fin.close();

    for (auto i : infshua)
    {
        cout << i.name << " " << i.price << " " << i.id << endl;
    }

    cin.get();
}

6. 文件指针移动:

  (1)移动到合适位置,读:

#include <iostream>
#include <fstream>

using namespace std;

void main()
{
    ofstream fout("C:\\3-文件操作练习\\3.txt");
    if (!fout)
    {
        cout << "文件操作失败!\n" << endl;
    }
    fout << "123456789abcdefghijklmnopqrstuvwxyz" ;
    fout.close();

    ifstream fin("C:\\3-文件操作练习\\3.txt");
    if (fin.fail())
    {
        cout << "文件操作失败!\n" << endl;
    }
    fin.seekg(9, ios::beg);//文件指针从开始移动9个位置 读
    char ch;
    while (fin.get(ch))
    {
        cout << ch;
    }
    fin.close();


    cin.get();
}

  (2)移动到合适位置,写:

#include <iostream>
#include <fstream>

using namespace std;

void main()
{
    ifstream fin("C:\\3-文件操作练习\\3.txt");
    if (fin.fail())
    {
        cout << "文件操作失败!\n" << endl;
    }
    char ch;
    while (fin.get(ch))
    {
        cout << ch;
    }
    fin.close();

    ofstream fout("C:\\3-文件操作练习\\3.txt");
    if (!fout)
    {
        cout << "文件操作失败!\n" << endl;
    }
    fout.seekp(5, ios::beg);
    fout << "hello world" << endl;
    fout.close();

    cin.get();
}

 

posted @ 2018-08-25 22:19  博观&约取  阅读(610)  评论(0编辑  收藏  举报