使用C ++ ifstream提取运算符>>从文件读取格式化的数据 [英] Using C++ ifstream extraction operator>> to read formatted data from a file

查看:162
本文介绍了使用C ++ ifstream提取运算符>>从文件读取格式化的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的学习,我试图使用c ++ ifstream和其操作符>>使用下面的代码从文本文件中读取数据。文本文件outdummy.txt包含以下内容:

As my learning, I am trying to use c++ ifstream and its operator>> to read data from a text file using code below. The text file outdummy.txt has following contents:

just dummy
Hello ofstream
555

我的问题是如何读取文件中存在的char数据为char数组或字符串。如何在下面的代码中使用ifstream :: operator >>。

My questions is how to read char data present in the file into a char array or string. How to do this using the ifstream::operator>> in code below.

#include <iostream>
#include <fstream>

int main()
{
    int a;
    string s;
    char buf[100];
    ifstream in("outdummy.txt",ios_base::in);


    in.operator>>(a); //How to read integer? How to read the string data.??

    cout << a;

    in.close();
    getchar();
    return 0;
}


推荐答案

输入,你必须提前知道什么数据期望和读取它变成相应的数据类型的变量。例如,如果您知道该数字始终是第五个令牌,如在您的示例中,您可以这样做:

If you want to use formatted input, you have to know in advance what data to expect and read it into variables of the according data type. For example, if you know that the number is always the fifth token, as in your example, you could do this:

std::string s1, s2, s3, s4;
int n;

std::ifstream in("outdummy.txt");

if (in >> s1 >> s2 >> s3 >> s4 >> n)
{
  std::cout << "We read the number " << n << std::endl;
}

另一方面,如果你知道数字总是在第三线本身:

On the other hand, if you know that the number is always on the third line, by itself:

std::string line;

std::getline(in, line);  // have line 1
std::getline(in, line);  // have line 2
std::getline(in, line);  // have line 3

std::istringstream iss(line);

if (iss >> n)
{
  std::cout << "We read the number " << n << std::endl;
}

正如您所看到的,要将令牌读取为字符串,它变成 std :: string 。重要的是记住格式化的输入操作符通过令牌工作令牌,令牌由空格(空格,制表符,换行符)分隔。通常的基本选择是,您是以完整的令牌(第一版本)还是逐行(第二版本)处理文件。对于逐行处理,您首先使用 getline 将一行读入字符串,然后使用字符串流对字符串进行标记。

As you can see, to read a token as a string, you just stream it into a std::string. It's important to remember that the formatted input operator works token by token, and tokens are separated by whitespace (spaces, tabs, newlines). The usual fundamental choice to make is whether you process a file entirely in tokens (first version), or line by line (second version). For line-by-line processing, you use getline first to read one line into a string, and then use a string stream to tokenize the string.

关于验证的词:您不能知道格式化提取是否实际上成功,因为这取决于输入数据。因此,您应该始终检查输入操作是否成功,如果没有成功,则中止解析,因为如果失败,您的变量将不包含正确的数据,但是您没有办法知道以后。所以总是这样说:

A word about validation: You cannot know whether a formatted extraction will actually succeed, because that depends on the input data. Therefore, you should always check whether an input operation succeeded, and abort parsing if it doesn't, because in case of a failure your variables won't contain the correct data, but you have no way of knowing that later. So always say it like this:

if (in >> v) { /* ... */ }            // v is some suitable variable
else { /* could not read into v */ }

if (std::getline(in, line)) { /* process line */ }
else { /* error, no line! */ }

后一种结构通常用于 c $ c> loop,按行读取整个文件:

The latter construction is usually used in a while loop, to read an entire file line by line:

while (std::getline(in, line)) { /* process line */ }

这篇关于使用C ++ ifstream提取运算符&gt;&gt;从文件读取格式化的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆