C++ ifstream 类
示例
使用 ifstream
从文件中读取行
// Create a text string, which is used to output the text file
string myText;
// Read from the text file
ifstream MyReadFile("filename.txt");
// Use a while loop together with the getline() function to read the file line by line
while (getline (MyReadFile, myText)) {
// Output the text from the file
cout << myText;
}
// Close the file
MyReadFile.close();
定义和用法
ifstream
类("输入文件流"的缩写)用于从文件中读取数据。
ifstream
类定义在 <fstream>
头文件中。
要打开一个文件,将文件路径传递给构造函数
ifstream MyReadFile("filename.txt");
ifstream
类有许多从文件中读取数据的方法。一个简单的方法是使用 getline()
函数读取所有字符直到下一个换行符,并将它们写入字符串。
输出文件中的一行文本
string myText;
getline(MyReadFile, myText);
cout << myText;
文件读取函数
文件读取函数从文件中提取字符并移动文件指针。
get()
get()
方法从文件中读取单个字符,并将其 ASCII 值作为 int
值返回。将其转换为 char
类型即可看到字符。文件指针会移动到文件中的下一个字符。
char myChar = MyReadFile.get();
cout << myChar;
get(destination, size, delimiter)
方法将最多 size 个字符从文件中读取并写入 destination。它会在遇到换行符、文件结尾或 delimiter 参数指定的可选字符时停止读取。写入 destination 的值总是以 \0
空终止符结尾。此方法会将文件指针移动到它停止读取的换行符或分隔符处。
char destination[20];
MyReadFile.get(destination, 20);
cout << destination << "\n";
// Stop reading when a '.' is found
MyReadFile.get(destination, 20, '.');
cout << destination << "\n";
getline()
getline(destination, size, delimiter)
方法与 get(destination, size, delimiter)
方法相同,只是它会丢弃换行符或分隔符,并将文件指针移动到其后面的字符。
char destination[20];
MyReadFile.getline(destination, 20);
cout << destination << "\n";
// Stop reading when a '.' is found
MyReadFile.getline(destination, 20, '.');
cout << destination << "\n";
有一个类似的 getline(stream, destination, delimiter)
**函数**,它从 stream 参数中指定的 ifstream
对象指向的文件中读取所有字符直到下一个换行符(或可选的 delimiter),并将它们写入 destination 指定的字符串中。
string destination;
getline(MyFile, destination);
cout << destination << "\n";
// Stop reading when a '.' is found
getline(MyFile, destination, '.');
cout << destination << "\n";
read()
read(destination, n)
方法从文件中读取 n 个字符,并将它们写入 destination 参数指定的 char
数组中。与其他函数不同,它不会在换行符处停止读取,也不会在数据末尾添加空终止符。
char destination[20];
MyReadFile.read(destination, 19);
destination[20] = '\0'; // Make sure it ends with a null terminating character
cout << destination << "\n";
peek()
peek()
方法从文件中读取单个字符,并将其 ASCII 值作为 int
值返回。将其转换为 char
类型即可看到字符。与 get()
方法不同,此方法不会移动文件指针。
char myChar = MyReadFile.peek();
cout << myChar;
gcount()
gcount()
方法返回最近一次调用的文件读取方法从文件中提取的字符数。
char destination[20];
MyReadFile.getline(destination, 20);
cout << MyReadFile.gcount() << "\n";
文件处理函数
文件处理函数用于打开、关闭和导航文件。
open()
open(filepath)
方法打开 filepath 指定路径的文件。如果文件已经打开,则此方法无效。
ifstream MyReadFile;
MyReadFile.open("filename.txt");
is_open()
is_open()
方法如果文件已打开则返回 true,如果没有文件打开则返回 false。
ifstream MyReadFile;
cout << MyReadFile.is_open(); << "\n"; // Displays 0 because the file is not open
MyReadFile.open("filename.txt");
cout << MyReadFile.is_open(); << "\n"; // Displays 1 because the file is open
close()
close()
方法关闭文件。完成文件操作后关闭文件以释放资源是一个好习惯。
MyReadFile.close();
rdbuf()
rdbuf()
方法返回一个指向内部 filebuf
对象的指针,该对象直接处理文件。
filebuf * buf = MyReadFile.rdbuf();
unget()
unget()
方法将文件指针向后移动一个字符。
使用 unget()
方法打印同一个字符两次
char myChar = MyReadFile.get();
cout << myChar << "\n";
MyReadFile.unget();
myChar = MyReadFile.get();
cout << myChar;
seekg()
seekg(position)
方法将文件指针移动到文件开头指定的位置。
MyReadFile.seekg(6)
seekg(position, origin)
方法将文件指针移动到文件中相对于 origin 的指定 position。origin 有三个可能的值:
ifstream::beg
- 位置相对于文件开头。ifstream::cur
- 位置相对于当前文件位置。ifstream::end
- 位置相对于文件结尾。
移动文件指针到不同位置
MyReadFile.seekg(6, ifstream::beg);
cout << MyReadFile.tellg(); << "\n";
MyReadFile.seekg(-3, ifstream::cur);
cout << MyReadFile.tellg(); << "\n";
MyReadFile.seekg(-4, ifstream::end);
cout << MyReadFile.tellg(); << "\n";
tellg()
tellg()
方法返回文件中文件指针的当前位置。
cout << MyReadFile.tellg();
提取运算符
>>
提取运算符从文件当前位置读取一定数量的字符,解释它们并将解释后的值写入变量。然后文件指针移动到下一个尚未读取的字符。字符的解释方式取决于变量的数据类型。
语法
MyReadFile >> variable
它也可以多次使用,以顺序读取文件的不同部分。
MyReadFile >> variable1 >> variable2 >> variable3
>>
提取运算符首先跳过空白字符(空格、制表符和换行符),直到遇到第一个非空白字符。之后,它将根据变量的数据类型遵循下表所示的规则。
数据类型 | 描述 | 示例 |
---|---|---|
int long short
|
读取一串数字并将其解释为整数。该串数字前面可以有一个符号("+" 或 "-")。它会在遇到第一个非数字字符时停止读取。 如果没有找到有效序列, ifstream 对象将失败并停止进一步读取。 |
15 |
bool |
读取一个整数(方式与上面描述的相同),然后将 0 解释为 *false*,将 1 解释为 *true*。任何其他整数值都将被解释为 *true*,但 ifstream 对象将失败并停止进一步读取。下一节中描述的 boolalpha 操纵符会完全改变此行为。 |
0 |
float double
|
读取有效的字符序列并将其解释为浮点数。有效序列至少有一个数字,前面可以有一个符号(“+”或“-”),后面可以跟一个小数点和十进制数字。也可以使用科学记数法(一个数字后跟“e”或“E”和一些数字)。 如果没有找到有效序列, ifstream 对象将失败并停止进一步读取。 |
5 |
char
|
从文件中读取单个字符。 如果文件指针已到达文件末尾, ifstream 对象将失败并停止进一步读取。 |
B |
string char *
|
读取直到下一个空白字符(空格、制表符或换行符)、空终止符或文件末尾的所有字符。变量的值将添加一个 \0 空终止符。如果文件指针已处于空终止符或文件末尾, ifstream 对象将失败并停止进一步读取。 |
Hello |
操作符
可以使用操纵符代替变量。使用操纵符时,它们会改变 ifstream
对象解释数据的方式。操纵符的效果会一直保持,直到被另一个操纵符更改。
下表列出了 ifstream
对象可以使用的一系列操纵符。
操作符 | 描述 |
---|---|
noskipws |
提取运算符 >> 将会读取空白字符,而不是跳过它们。这对于 char 类型变量很有用,因为对于其他数据类型,它会在遇到空白字符时停止读取。 |
skipws |
重置由 noskipws 操纵符所做的更改。 |
ws |
将文件指针移动到文件中的下一个非空白位置。 |
hex |
在使用整数变量时,期望十六进制表示(数字 0-9 和 A-F)的数字。 |
oct |
在使用整数变量时,期望八进制表示(数字 0-7)的数字。 |
dec |
在使用整数变量时,期望十进制表示(数字 0-9)的数字。这会重置由 hex 和 oct 操纵符所做的更改。 |
boolalpha |
读取布尔变量的数据时,它不会查找整数,而是查找字符串 "true" 或 "false"。 |
noboolalpha |
重置由 boolalpha 操纵符所做的更改。 |
示例
使用操纵符更改数据解释方式
bool myBool;
int myInt;
// Interpret character sequences "true" and "false" as boolean values
MyFile >> boolalpha >> myBool;
// Revert to reading booleans normally
MyFile >> noboolalpha;
// Read hexadecimal numbers from the file and interpret them as integers
MyFile >> hex >> myInt;
// Revert to reading integers normally
MyFile >> dec;