C++ 文件读取与写入

先上一个简单的例子:

代码

ngnsvr9 [** NONE **]/home/xionghailong/c++/file $ cat file.cpp
#include <fstream>
#include <iostream>

using namespace std;
int main()
{
        ifstream file_reader ("file.txt");
        if (! file_reader.is_open())
        {
                cout<<"could not open file"<<'\n';
        }
        else
        {
                cout<<"have opened file"<<'\n';
        }

        int data;
        if(file_reader >> data)
        {
                cout << "the data is : "<<data<<'\n';
        }
        else
        {
                cout<<"read data error."<<'\n';
        }
}

处理的file.txt:

ngnsvr9 [** NONE **]/home/xionghailong/c++/file $ cat file.txt
123 a ab abc

编译执行:

ngnsvr9 [** NONE **]/home/xionghailong/c++/file $ g++ file.cpp
ngnsvr9 [** NONE **]/home/xionghailong/c++/file $ ./a.out
have opened file
the data is : 123

上例中实现读入一个文件中的数据,并打印;如果读入的数据与定义的类型不匹配,将输出“read data error.”;

文本读入是以空格符号(可以为其他符号)作为分隔符。 

 

下面来一个新例子

代码:

ngnsvr9 [** NONE **]/home/xionghailong/c++/file $ cat read_file.cpp
#include <fstream>
#include <iostream>
#include <vector>

using namespace std;

int main()
{
        ifstream file_reader ("score.txt");
        if (! file_reader.is_open())
        {
                cout<<"could not open file"<<'\n';
        }
        else
        {
                cout<<"have opened file"<<'\n';
        }

        vector<int> scores;
        for (int i=0; i<10; i++)
        {
                int score;
                if( ! (file_reader >> score))   //一定要打个括号,‘!’优先级很高
                {
                        break;
                }
                cout << "the score is : "<<score<<'\n';
                scores.push_back(score);
        }

                vector<int>::iterator it;

                for(it=scores.begin(); it != scores.end(); it++)
                {
                        cout <<*it<<'\n';
                }

}


ngnsvr9 [** NONE **]/home/xionghailong/c++/file $ cat score.txt
12
123
11
22
33
44
111
222
444
555

编译并执行:

ngnsvr9 [** NONE **]/home/xionghailong/c++/file $ g++ read_file.cpp
ngnsvr9 [** NONE **]/home/xionghailong/c++/file $ ./a.out
have opened file
the score is : 12
the score is : 123
the score is : 11
the score is : 22
the score is : 33
the score is : 44
the score is : 111
the score is : 222
the score is : 444
the score is : 555
12
123
11
22
33
44
111
222
444
555

上例中将score.txt中的数据读入,并且存入vector<int> scores,最后打印存入的数据。if( ! (file_reader >> score))  这一步,

一定要加个括号,非的优先级比较高,否则会出错!

 

文件的写入:

ngnsvr9 [** NONE **]/home/xionghailong/c++/file/write $ cat write_file.cpp
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
        ofstream file_writer("data.txt", ios::app);
     if (! file_writer.is_open() )
     {
        cout<<"could not open file"<<'\n';
        return 0;
    }
   
    for( int i=0; i<10; i++)
    {
            file_writer << 10-i << '\n';
    }
}

ngnsvr9 [** NONE **]/home/xionghailong/c++/file/write $ cat data.txt
10
9
8
7
6
5
4
3
2
1
10
9
8
7
6
5
4
3
2
1

如果没有data.txt,运行时会自动创建文件;ofstream 接收第二个参数。

ios::app   在文件后面作添加,每次写入之后把位置设到最后

ios::ate     把当前位置设为最后

ios::trunc  删除文件中所有东西

ios::out     允许向文件输入

ios::binary  允许对流进行二进制操作,读取文件同样可以这样

 

eg:

ofstream a_fie("data.txt", ios::app | ios::binary)

 

比较基础,希望对你有所帮助!

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值