如何使用 ostream& 将文件提取的数据传递到数组中到打印函数? (C++)

问题描述 投票:0回答:1

我正在尝试使用两个功能 -

  1. 从文本文件中读取数据并将其存储到结构体格式化的数组中
  2. 通过ostream&打印数组数据
const int maxRows =20;
//3 columns across

struct someThing{
string a;
int b;
int c;
}

void readFile (someThing arr[], int x) {
    //do stuff
    //open file...
    //loop 
}

void printArray (ostream&, someThing thing){
    
     //?? I am not sure what to here...

     //cout <<thing.a <<','<<thing.b <<','<<thing.c <<endl;

     for (int i = 0; i < maxRows; i++){
        cout << thing[i];

}

文本文件格式如下。

thing1 15.30 48
thing2 20.15 78
thing3 108.23 56

等等...20行

从技术上讲,仍然有一个枚举类,但我必须先重新处理它,但首先是基础知识。

我尝试过: -使用

ostream& os
然后
os<<thing[i];
我只是遇到操作员过载问题和引用错误。 -我能想到的尽可能多的关键词。 -我浏览了 cplusplus 论坛。 -读了我的C++书,这让我更加困惑。 -我在枚举类上找不到太多帮助,所以我把它拿出来了。

   switch (thing.size){
        case thingType:: S: "Small"; break;
        case thingType::M: "Medium"; break;
        case thingType::L: "Large"; break;
    }
c++ multidimensional-array struct pass-by-reference iostream
1个回答
0
投票

你不应该使用“C”风格的数组,C++ 的可调整大小的数组

std::vector
。另外,C++ 中的文件 IO 通常是通过流处理的。
无论何时,您都应该使用
range based for loops
而不是基于索引的(以避免意外超出数组范围)。

总而言之,它看起来像这样:

#include <iostream>
#include <string>
#include <sstream>
#include <vector> // don't use "C" style arrays like you do this is C++ after all

// don't use `using namespace std;` 
//const int maxRows =20; No with std::vector we can read any number of lines

struct someThing
{
    std::string a;
    int b;
    int c;
};

// In C++ when dealing with files you might as well 
// generalize to streams.
// So create overloads for streaming someThing from and to streams

std::istream& operator>>(std::istream& is, someThing& st)
{
    is >> st.a >> st.b >> st.c;
    return is;
}

std::ostream& operator<<(std::ostream& os, const someThing& st)
{
    os << st.a << " " << st.b << " " << st.c;
    return os;
}

// Now we use the power of the C++ resizable array
// std::vector to load data from a stream (line by line)
std::vector<someThing> Load(std::istream& is)
{
    std::vector<someThing> things;
    someThing thing;
    std::string line;

    while (getline(is, line))
    {
        std::istringstream iss(line);
        iss >> thing;
        things.push_back(thing);
    }
   
    return things;
}

// Saving is now the same as writing each thing to a stream
void Save(std::ostream& os, const std::vector<someThing>& things)
{
    for (const auto& thing : things)
    {
        os << thing << "\n";
    }
}

// simulate a file 
// when doing testing don't rely on your file system
// just simulate the file in memory using a stringstream
std::stringstream is
{
    "thing1 15.30 48\n"
    "thing2 20.15 78\n"
    "thing3 108.23 56\n"
};

int main()
{
    // load your things from any stream
    // you can replace this with a std::ifstream
    // in production code
    auto things = Load(is);
    
    // save to std::cout for testing 
    // replace with std::ofstream in production code
    Save(std::cout, things);

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.