无法读取结构向量的数据

时间:2016-02-25 01:53:56

标签: c++ fstream

我正在编写这些代码来从文件中读取数据然后写入结构中定义的各种向量。你能告诉我为什么我不能使用另一个函数访问它们(在这种情况下是write_table)。感谢

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

using namespace std;

void read_data (string file_name);
void write_table ();

struct Record {
    vector <string> myv;
    vector <string> bwv;
};

int main ()
{
    read_data("data.txt");
    Record o;

    write_table (o);

}

void read_data (string file_name)
{
    Record r;

    ifstream data_in (file_name.c_str());
    static ofstream data_out ("tuna.txt");
    if (!data_in)
    {
        cout<<"Failed to open file"<<endl;
    }
    else
    {
        string dummyline;
        getline (data_in, dummyline);
        string  my, bw;
        while (data_in>>my>>bw)
        {
            {
            r.myv.push_back(my);
            r.bwv.push_back(bw);
            }
        }
        data_in.close();
    }
    data_out<<"MY"<<'\t'<<"BW"<<endl;
    size_t size=r.myv.size();
    for (size_t i=0; i<size; i++)
    {
        cout<<r.myv[i]<<'\t'<<r.bwv[i]<<endl;
        //data_out<<r.myv[i]<<'\t'<<r.bwv[i]<<endl;
    }


}


void write_table (Record bo)
{

   size_t size = bo.myv.size();
    for (size_t i=0; i<size; i++)
    {
        cout<<bo.myv[i]<<'\t'<<bo.bwv[i]<<endl;

    }

}

我按照以下方式重新组织我的节目作为Songyuanya的建议&gt;我的明确问题(作为James的建议)是:我想在类Record中编写另一个函数write_table,打印出两个向量:myv和bwv来筛选/或稍后将它们写入文件。感谢

//主要cpp:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Record.h"
using namespace std;

int main ()
{
    Record r;
    r.read_data("data.txt");

}

Record.h

#ifndef RECORD_H
#define RECORD_H
#include <vector>
#include <string>
using namespace std;

class Record
{
    public:
        Record();
        void read_data (string file_name);

    private:
    vector <string> myv;
    vector <string> bwv;
};

#endif // RECORD_H

Record.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "Record.h"
using namespace std;


Record::Record()
{
    //ctor
}
void Record::read_data (string file_name)
{
    ifstream data_in (file_name.c_str());
    static ofstream data_out ("tuna.txt");
    if (!data_in)
    {
        cout<<"Failed to open file"<<endl;
    }
    else
    {
        string dummyline;
        getline (data_in, dummyline);
        string  my, bw;
        while (data_in>>my>>bw)
        {
                {
                myv.push_back(my);
                bwv.push_back(bw);
                }
        }
        data_in.close();
    }
}

1 个答案:

答案 0 :(得分:0)

您需要通过参数或返回值在Recordread_data之间传递write_table的对象。

如果它由read_datawrite_table共享和处理,为什么不制作read_datawrite_table成员函数并将它们封装到一个类中?

class Record {
private:
    vector <string> myv;
    vector <string> bwv;
public:
    void read_data (string file_name);
    void write_table ();
};

void Record::read_data (string file_name) {
    // using myv and bwv...
}

void Record::write_table () {
    // using myv and bwv...
   size_t size = myv.size();
   for (size_t i=0; i<size; i++)
   {
       cout<<myv[i]<<'\t'<<bwv[i]<<endl;
   }
}

int main ()
{
    Record o;
    o.read_data("data.txt");
    o.write_table();
}