Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 从文件中逐字读取_C++ - Fatal编程技术网

C++ 从文件中逐字读取

C++ 从文件中逐字读取,c++,C++,我有一门课是食物,它的h是: class Food{ private: string name; int protein; int calories; int fats; string type; public: .... friend istream & operator >> (istream &i, Food & other); }; 因此,使用operator>>我想从具有以下格式的文件中读取一种食物的属性: Ketchup;98

我有一门课是食物,它的h是:

class Food{

private:

string name;

int protein;

int calories;

int fats;

string type;

public:

....

friend istream & operator >> (istream &i, Food & other);

};
因此,使用operator>>我想从具有以下格式的文件中读取一种食物的属性:

Ketchup;98;24;2;Processed
我试着做:

istream & operator >> (istream &i, Food & other){
    string nom, t;

    int c, p, h, g, f;

    char aux;   //Aux stores ';'

    i >> nom >> aux >> c >> aux >> p >> aux >> f;

    getline(i,t); //I read the type and change the line that I'm reading

    Food ing(nom.c_str(),c,p,f,t.c_str()); //Constructor

    other = ing;  //Overload of operator =

    return i;
}

但我没有成功,请提供帮助?

似乎您想读取csv数据。这是一个标准的任务,我会给你详细的解释。最后,所有读数将在一行中完成

我建议使用一种“现代”C++方法。< /P> 尽管如此,所有谈论csv的人都链接到了,这些问题是从2009年开始的,现在已经超过10年了。大多数答案都是老生常谈的,而且非常复杂。所以,也许是时候改变了

在现代C++中,你有迭代遍历范围的算法。您经常会看到类似“someAlgoritm(container.begin()、container.end()、someLambda)”的内容。我们的想法是迭代一些类似的元素

在您的例子中,我们迭代输入字符串中的标记,并创建子字符串。这称为标记化

正是出于这个目的,我们有了
std::sregex\u令牌\u迭代器
。因为我们有这样的定义,我们应该使用它

这是一个迭代器。用于在字符串上迭代,因此是sregex。开始部分定义了我们将在什么范围内操作输入,然后在输入字符串中有一个
std::regex
来表示应该匹配的内容或不应该匹配的内容。最后一个参数给出了匹配策略的类型

  • 1-->给我在正则表达式中定义的内容,然后
  • -1-->根据正则表达式给出不匹配的内容
因此,现在我们了解了迭代器,我们可以将标记从迭代器std::复制到我们的目标,即
std::string
std::vector
。由于我们不知道我们有多少列,我们将使用
std::back_inserter
作为目标。这将添加我们从
std::sregex\u token\u迭代器
获得的所有令牌,并将其附加到我们的
std::vector>
中。我们有多少专栏并不重要

好。这样的声明可能看起来像

std::copy(                          // We want to copy something
    std::sregex_token_iterator      // The iterator begin, the sregex_token_iterator. Give back first token
    (
        line.begin(),               // Evaluate the input string from the beginning
        line.end(),                 // to the end
        re,                         // Add match a semicolon
        -1                          // But give me back not the comma but everything else 
    ),
    std::sregex_token_iterator(),   // iterator end for sregex_token_iterator, last token + 1
    std::back_inserter(cp.columns)  // Append everything to the target container
);
现在我们可以理解这个复制操作是如何工作的了

下一步。我们想从文件中读取。该文件还包含某种相同的数据。相同的数据是行

对于上面,我们可以迭代类似的数据。如果是文件输入或其他。为此,C++具有<代码> STD::istRAMMyTyror < /C>。这是一个模板,作为模板参数,它获取应该读取的数据类型,作为构造函数参数,它获取对输入流的引用。如果输入流是
std::cin
,或
std::ifstream
std::istringstream
,则无所谓。所有类型的流的行为都是相同的

由于我们没有SO文件,所以(在下面的示例中)我使用
std::istringstream
来存储输入的csv文件。当然,您可以通过定义
std::ifstream testCsv(filename)
来打开文件。没问题

使用std::istream_迭代器,我们对输入进行迭代并读取类似的数据。在我们的例子中,一个问题是我们想要迭代特殊数据,而不是某些内置数据类型

为了解决这个问题,我们定义了一个代理类,它为我们完成内部工作(我们不想知道如何封装在代理中)。在代理中,我们覆盖类型转换操作符,以获得
std::istream\u迭代器的预期类型的结果

最后一个重要步骤。
std::vector
具有范围构造函数。它还有许多其他构造函数,我们可以在定义
std::vector
类型的变量时使用它们。但就我们的目的而言,这个构造函数最适合我们

所以我们定义一个变量csv,并使用它的范围构造函数,给它一个范围的开始和结束。在我们的具体示例中,我们使用
std::istream\u迭代器的开始迭代器和结束迭代器

如果我们结合以上所有内容,读取完整的CSV文件是一个线性过程,它是一个变量的定义,并调用其构造函数

请参阅生成的代码:

#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <regex>
#include <algorithm>

std::istringstream testCsv{ R"(Ketchup;98;24;2;Processed
Fries;10;20;2;Processed
Meat;10;20;2;Processed)" };


// Define Alias for easier Reading
using Columns = std::vector<std::string>;
using CSV = std::vector<Columns>;


// Proxy for the input Iterator
struct ColumnProxy {    
    // Overload extractor. Read a complete line
    friend std::istream& operator>>(std::istream& is, ColumnProxy& cp) {

        // Read a line
        std::string line; cp.columns.clear();
        std::getline(is, line);

        // The delimiter
        const std::regex re(";");

        // Split values and copy into resulting vector
        std::copy(std::sregex_token_iterator(line.begin(), line.end(), re, -1),
            std::sregex_token_iterator(),
            std::back_inserter(cp.columns));
        return is;
    }

    // Type cast operator overload.  Cast the type 'Columns' to std::vector<std::string>
    operator std::vector<std::string>() const { return columns; }
protected:
    // Temporary to hold the read vector
    Columns columns{};
};


int main()
{
    // Define variable CSV with its range constructor. Read complete CSV in this statement, So, one liner
    CSV csv{ std::istream_iterator<ColumnProxy>(testCsv), std::istream_iterator<ColumnProxy>() };

    // Print result. Go through all lines and then copy line elements to std::cout
    std::for_each(csv.begin(), csv.end(), [](Columns& c) {
        std::copy(c.begin(), c.end(), std::ostream_iterator<std::string>(std::cout, " ")); std::cout << "\n";   });
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
标准::istringstream testCsv{R“(番茄酱;98;24;2;加工
炸薯条;10;20;2;加工
肉类; 10 ; 20 ; 2 ;加工;;
//定义别名以便于阅读
使用Columns=std::vector;
使用CSV=std::vector;
//输入迭代器的代理
结构列代理{
//重载提取器。读取完整的行
friend std::istream&operator>>(std::istream&is、ColumnProxy&cp){
//读一行
std::string行;cp.columns.clear();
std::getline(is,line);
//分隔符
常量std::正则表达式re(“;”);
//拆分值并复制到结果向量中
std::copy(std::sregex_token_迭代器(line.begin(),line.end(),re,-1),
std::sregex_token_迭代器(),
std::背面插入器(cp列);
回报是;
}
//类型转换运算符重载。将类型“Columns”转换为std::vector
运算符std::vector()常量{返回列;}
受保护的:
//临时保存读取向量
列{};
};
int main()
{
//定义变量CSV及其范围构造函数。请阅读此语句中的完整CSV,因此,一行
CSV CSV{std::istream_迭代器(testCsv),std::istream_迭代器();
//打印结果。检查所有行,然后将行元素复制到std::cout
std::for_each(csv.begin()、csv.end()、[](列和c){

std::copy(c.begin()、c.end()、std::ostream_迭代器(std::cout,“”);std::cout似乎您想要读取csv数据。这是一项标准任务,我将给您详细解释。最后,所有读取都将在一行中完成

我建议使用一种“现代”C++方法。< /P> 还有