C++中ifstream使用笔记(一)(常用方法和注意事项)

ifstream简介: C++平台用来文件操作的库

std::ifstream



常用方法:

open(): ifstream关联文件的方式有两种,通过ifstream构造函数以及通过open来打开一个文件流

example:

ifstream input_file(FILE_NAME);

// OR

ifstream input_file2;
input_file2.open(FILE_NAME, ifstream::in); //ifstream::in 是打开的mode,有多种打开mode,见下文
mode     描述
in *读取文件
out写入模式,适用于output
binary二进制模式
ate起点设置在文件的结尾  at the end of file
app在文件的结尾进行文件的操作,写入.
trunc放弃所有文件之前的内容(很危险!会清空文件内容).



close()   // 关闭一个文件


get_line()

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

获取文件流中的一行数据,保存为一个c字符串形式(char *),结束标志是换行符 \n,n表示能写入s的最大字符数量(包括'\0')

delim参数:当匹配这个指定的字符的时候,结束数据的写入(这种情况不会将failbit位置为true,文件读取成功)

注意:

1. 如果读取的是一个空字串,而n大于0的话,会在s中自动写入一个字串结束标识符 '\0'

2. 如果读取的字符大于n或者等于n,但是这个时候并没有到达这行的结尾,也就是没有到换行符的位置,说明这行数据并不完整,这时候failbit会被置为true


eof()

检查eofbit是true还是false,以此来判断文件是否读取完毕(到文件的EOF位,算是end_of_file)


clear()

清楚错误信息,将goodbit设置为true

iostate value
(member constant)
indicatesfunctions to check state flags
good()eof()fail()bad()rdstate()
goodbitNo errors (zero value iostate)truefalsefalsefalsegoodbit
eofbitEnd-of-File reached on input operationfalsetruefalsefalseeofbit
failbitLogical error on i/o operationfalsefalsetruefalsefailbit
badbitRead/writing error on i/o operationfalsefalsetruetruebadbit


代码示例:

功能:将file1中的每一行单独读取并保存到string容器中

#include<fstream>
#include<string>
#include<vector>
#include<iostream>

#define FILE_NAME "file1"

using namespace std;

int main()

{

        ifstream input_file;

        vector<string> ivec;

        string s;

        input_file.open(FILE_NAME, ifstream::in);

        while(!input_file.eof())

        {

                getline(input_file, s);

                ivec.push_back(s);

                //cout << s.c_str() << endl;

        }

        vector<string>::const_iterator itor = ivec.begin();

        for(itor;itor != ivec.end(); itor ++)

        {

                cout << *itor << endl;

        }

        return 0;

}



  • 11
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++,`ifstream`是用于从文件读取数据的输入流类。它是`istream`类的派生类,提供了一些特定于文件输入的功能。 要使用`ifstream`,首先需要包含头文件`<fstream>`。然后可以创建一个`ifstream`对象,并将文件名作为参数传递给构造函数。例如,以下代码创建了一个名为`file`的`ifstream`对象,并打开名为`example.txt`的文件进行读取: ```cpp #include <fstream> int main() { std::ifstream file("example.txt"); // 读取文件内容... return 0; } ``` 一旦文件被打开,可以使用`ifstream`对象的成员函数来读取文件内容。以下是一些常用的成员函数: - `getline()`:从文件读取一行文本。 - `get()`:从文件读取一个字符。 - `>>`:从文件读取数据,可以用于读取各种类型的数据,如整数、浮点数等。 - `eof()`:检查是否已到达文件末尾。 下面是一个示例,演示了如何使用`ifstream`从文件读取内容并输出到控制台: ```cpp #include <iostream> #include <fstream> int main() { std::ifstream file("example.txt"); if (file.is_open()) { std::string line; while (std::getline(file, line)) { std::cout << line << std::endl; } file.close(); } else { std::cout << "Failed to open the file." << std::endl; } return 0; } ``` 在上面的示例,我们使用`getline()`函数从文件逐行读取内容,并将每行内容输出到控制台。最后,我们使用`close()`函数关闭文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值