以下文本从供应商列表文件中读取文本。
vendor.h
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
class Vendor
{
public:
//data types
int VendorID;
string VendorName;
string VendorCity;
string VendorState;
int VendorZip;
//constructors
Vendor(int ID, string Name, string City, string State, int Zip){ VendorID = ID, VendorName = Name, VendorCity = City, VendorState = State, VendorZip = Zip;}
Vendor(){};
//desturcutor
~Vendor();
void SetID(int ID);
void SetName(string name);
void SetCity(string city);
void SetState(string state);
void SetZip(int zip);
};
#include "Vendor.h"
void main ()
{
Vendor VenList[150];
ifstream GetText;
GetText.open("VendorListFile.txt");
if (!GetText.is_open())
{
cout << "File could not be opened" << endl;
exit(EXIT_FAILURE);
}
while (GetText.good())
{
cout << element << " ";
GetText >> element;
}
}
显然,代码可以在文本中读取,但我现在要做的是遍历每个元素并将它们传递给供应商类的构造函数,以将元素构建到将保存在数组中的供应商中。我想用以下的while循环来做这个。
int counter = 150;
while (counter > 0)
{
Vendor (name = [counter + 1], state = [counter + 2] ... );
counter -= 5;
}
下一个问题是假设这些构造,我将如何在内存中找到这些供应商来索引或移动它们,以及如何使元素1中的元素读取元素之间的空格。 while循环是伪代码,所以我想我做了一些语法错误
答案 0 :(得分:1)
让我们考虑以下VendorListFile.txt
:
1 Intel Chicago IL 12345
2 Dell Seattle WA 23456
3 HP Paris FR 34567
请注意,鉴于您当前的实现(例如固定大小的供应商数组),此文件不应超过150行,否则您的程序在解析时会崩溃。
然后,您可以使用以下调整后的代码进行解析,将供应商存储为VenList数组中的对象,并在方便时迭代此数组:
// Vendor.h
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
class Vendor
{
public:
//data types
int VendorID;
string VendorName;
string VendorCity;
string VendorState;
int VendorZip;
//constructors
Vendor(int ID, string Name, string City, string State, int Zip){ VendorID = ID, VendorName = Name, VendorCity = City, VendorState = State, VendorZip = Zip;}
Vendor(){};
//desturcutor
~Vendor();
void SetID(int ID);
void SetName(string name);
void SetCity(string city);
void SetState(string state);
void SetZip(int zip);
};
::std::istream & operator >> (::std::istream & stream, Vendor & vendor);
::std::ostream & operator << (::std::ostream & stream, const Vendor & vendor);
// Vendor.cpp
Vendor::~Vendor() {}
::std::istream & operator >> (::std::istream & stream, Vendor & vendor) {
stream >> vendor.VendorID;
stream >> vendor.VendorName;
stream >> vendor.VendorCity;
stream >> vendor.VendorState;
stream >> vendor.VendorZip;
return stream;
}
::std::ostream & operator << (::std::ostream & stream, const Vendor & vendor) {
stream << "ID: " << vendor.VendorID << ::std::endl;
stream << "Name: " << vendor.VendorName << ::std::endl;
stream << "City: " << vendor.VendorCity << ::std::endl;
stream << "State: " << vendor.VendorState << ::std::endl;
stream << "ZIP: " << vendor.VendorZip << ::std::endl;
return stream;
}
// main.cpp
int main ()
{
Vendor VenList[150];
ifstream GetText;
GetText.open("VendorListFile.txt");
if (!GetText.is_open())
{
cout << "File could not be opened" << endl;
exit(EXIT_FAILURE);
}
// Store the vendors as objects in the VenList array
int nVendors = 0;
GetText >> VenList[nVendors];
while (GetText.good())
{
cout << VenList[nVendors];
++nVendors;
GetText >> VenList[nVendors];
}
// Iterate the vendor objects from the VenList array
for (int i = 0; i < nVendors; ++i) {
cout << VenList[i];
}
return EXIT_SUCCESS;
}
为了简单起见,我将所有内容合并到一个文件中,随意组织3个文件中的代码/如果您愿意,可以添加必要的#include
。
请注意,我们实际上不需要使用专门的构造函数,因为在我们实例化VenList数组时,已经使用默认构造函数构造了150个供应商对象。因此,我们使用我们重载的&gt;&gt;读取文件时修改这些对象。操作
代码在Gentoo Linux上编译并运行GCC 4.8.2。运行它会提供以下输出:
ID: 1
Name: Intel
City: Chicago
State: IL
ZIP: 12345
ID: 2
Name: Dell
City: Seattle
State: WA
ZIP: 23456
ID: 3
Name: HP
City: Paris
State: FR
ZIP: 34567
ID: 1
Name: Intel
City: Chicago
State: IL
ZIP: 12345
ID: 2
Name: Dell
City: Seattle
State: WA
ZIP: 23456
ID: 3
Name: HP
City: Paris
State: FR
ZIP: 34567
在此示例中,供应商打印两次:一次来自原始解析循环,另一次打印VenList数组。
这里的关键是功能:
如果更改供应商类/文件结构,则需要适当调整这些功能。
希望它有所帮助。