c++ - 如何将文件的特定部分读入私有(private)数据成员

标签 c++ class file-handling

我正在尝试读取 virtual void BillRecord::ReadCustDetails(ifstream &fin) 中包含客户详细信息的文本文件。代码工作正常,但我什至不知道如何读取整个地址和负值并将它们存储给私有(private)成员。我尝试了以下代码,但输出错误。

文本文件:

Phone
Origin
George Carter
24 Dingo St Exeter SA
-0.018
28

Gas
EA
Paul Scott
21 Beach Rd Barham NSW
15.48786
356567

Elect
...

我的输出:

Origin                         
George
Carter
0
24

要求的输出:

Origin
George Carter
24 Dingo St Exeter SA
-0.018
28

我的程序:

  public:
    BillRecord();
    virtual void ReadCustDetails(ifstream &fin);
  private:
    BillType BType;
    string Supplier; // Supplier's name
    string Name, Address; // Customer's name and address
    double BillAmount;// Amount in dollars and cents of this bill            
    int DaysSinceLastReading; // Days since last reading

};

 virtual void BillRecord::ReadCustDetails(ifstream &fin)
 {
        fin >> Supplier;
        getline(fin,Name);
        getline(fin,Address);
        fin >>AccountBalance;
        fin >>BillAmount;
        fin >>DaysSinceLastReading;
        fin >>i;
        DaysSinceLastReading=i;

    //put the code here for reading the customer details part of the file 
record only into the private data members

cout << Supplier<< endl;
cout << Name  << endl;
cout << Address << endl;
cout << BillAmount << endl;
cout << AccountBalance << endl;
}

最佳答案

您的代码只有小问题。尝试更具体。除了这段代码之外,真正的问题是什么?你一定在其他功能上有问题。尝试将它们张贴在这里。我已尽力修复。

void BillRecord::ReadCustDetails(ifstream &fin)
{

fin >> Supplier;
fin.ignore();
getline(fin,Name);
getline(fin,Address);
fin >> BillAmount;
fin >> AccountBalance;
fin >> DaysSinceLastReading;
cout << DaysSinceLastReading;

cout << Supplier << setw(16);
cout << Name     << setw(27);
cout << Address  << setw(15);
cout << BillAmount << setw(14);
cout << AccountBalance     << setw(8);
cout << DaysSinceLastReading  << setw(6);
}

关于c++ - 如何将文件的特定部分读入私有(private)数据成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50598967/

相关文章:

C# 使用我自己的 C++/CLI DLL : Error: 'mytrainOp' is not supported by the language

c# - 如何从单独的类(class)中打开/运行类(class)?

c++ - 如何将访问非全局类的全局函数放在一个地方?

java - 如何从txt文件中根据最小数量获取5个项目

c - 如何将数字从 .txt 文件加载到 C

c# - 为什么这在 C++ 中要慢得多?

c++ - vector<pair<int, unordered_set<int>>> 在为 pair 提供默认构造函数参数时给出段错误

HTML - 像 Web 浏览器通常那样加载图像

c++ - 从 STL 列表中删除特定节点

javascript - 向所有 Angular 模型添加子类