我正在尝试获取一个文件并将其放入3个不同的数组中。这些阵列中的两个是1D阵列,另一个是2D阵列。文本文件如下
Bill Hansley 1 1 1 1 1
Todd Howard 2 3 1 0 0
Sam Duke 0 1 1 0 0
Danny Martin 1 0 2 0 1
我正在尝试获取此文本文件,并将其名字插入到名为firstNames []的数组中,然后将另一个用于姓氏的数组称为lastNames [],最后将其插入数字,我希望将它们放在数组中称为Productsorders [] []。我的代码如下。
bool loadOrderFile(string orderFN,
string firstNames[], string lastNames[],
int productsOrders[MAX_ORDERS][MAX_PRODS],
int &namesCount, int &prodCount, string &menuName)
{
ifstream File;
File.open(orderFN.c_str());
if (File.is_open()) {
cout << "Order file opened..." << endl;
}
int i = 0;
getline(File, menuName);
(File >> prodCount);
while (File) {
File.get();
(File >> firstNames[i]);
(File >> lastNames[i]);
(File >> productsOrders[i][i]);
(File >> productsOrders[i + 1][i + 1]);
(File >> productsOrders[i + 2][i + 2]);
(File >> productsOrders[i + 3][i + 3]);
(File >> productsOrders[i + 4][i + 4]);
(i++);
}
cout << "Menu name: " << menuName << endl;
cout << "Product Count: " << prodCount << endl;
cout << "There were " << (prodCount - 1) << " orders read in." << endl;
for (int i = 0; i < 10; i++) {
cout << productsOrders[i][i] << endl;
}
for (int i = 0; i < 10; i++) {
cout << firstNames[i] << lastNames[i] << endl;
}
return true;
}
名称数组似乎可以正常工作,因为它们可以按原样输出名称,但是2D数组可以输出
1
2
0
1
0
2
0
1
0
0
应该在什么时候
1 1 1 1 1
2 3 1 0 0
0 1 1 0 0
1 0 2 0 1
我将不胜感激。
答案 0 :(得分:2)
您的问题是您在此处无法正确处理2D阵列。
例如,在3x3
2D数组中,您有两个索引[a][b]
,该数组的2D表示如下所示:
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
例如,当您输出时:
for (int i = 0; i < 10; i++) {
cout << productsOrders[i][i] << endl;
}
您会看到您只会在阵列中得到一条对角线,而不是所有项([0][0], [1][1], [2][2]
)。要打印整个数组,您需要使用两个循环。
输入有一个类似的问题,即同时增加两个索引。
答案 1 :(得分:0)
诚实地解析文本文件时;我将根据其各自的职责将逻辑分为各个功能。
您的代码应如下所示:
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <exception>
// Structure of your data type...
struct Order {
std::string firstName;
std::string lastName;
std::vector<int> productOrders; // maybe use vector instead of array...
// any other variable(s) or container(s) you may need...
};
// Simple ostream operator<< overload to print your Order Struct
// in a nice readable format.
std::ostream& operator<<(std::ostream& os, const Order& order );
// Function to split a string based on a single character delimiter
std::vector<std::string> splitString( const std::string& s, char delimiter );
// Function that will get each line text from a file and stores it into a vector
// of strings. Then closes the file handle once the entire file has been read.
void getAllLinesFromFile(const char* filename, std::vector<std::string>& output);
// This function will parse a single line of text or string that is contained in
// the vector of strings. The declaration of this can vary; if you have other
// information such as header information before the actual data structures
// you would have to modify this function's declaration-definition to accommodate
// for those variables.
void parseLine( const std::vector<std::string>& fileContents, std::vector<Order>& orders );
// Simple and clean looking main function.
int main() {
try {
std::vector<std::string> fileContents;
getAllLinesFromFile( "filename.txt", fileContents );
std::vector<Order> orders;
parseLine( fileContents, orders );
for ( auto& o : orders )
std::cout << o << '\n';
} catch( std::runtime_error& e ) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
std::ostream& operator<<(std::ostream& os, const Order& order ) {
os << order.firstName << " " << order.lastName << '\n';
for (auto& p : order.productOrders)
os << p << " ";
os << '\n';
return os;
}
std::vector<std::string> splitString( const std::string& s, char delimiter ) {
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream( s );
while( std::getline( tokenStream, token, delimiter ) ) {
tokens.push_back( token );
}
return tokens;
}
void getAllLinesFromFile(const char* filename, std::vector<std::string>& output) {
std::ifstream file(filename);
if (!file) {
std::stringstream stream;
stream << "failed to open file " << filename << '\n';
throw std::runtime_error(stream.str());
} else {
std::cout << "File " << filename << " opened successfully.\n";
}
std::string line;
while (std::getline(file, line)) {
if (line.size() > 0)
output.push_back(line);
}
file.close();
}
void parseLine( const std::vector<std::string>& fileContents, std::vector<Order>& orders ) {
// Here is where you would do the logic to parse the vector of strings
// this function may vary based on your file structure. If there is any
// header information you would have to extract that first from the
// vector's index.
// Once you get to the index in the vector that describes your data structure
// it is hear that you would want to call `splitString()` using the current
// current index of that vector and the space character as your delimiter.
// This will create a vector of strings that are now considered to be tokens.
// On each pass of the loop for each line of contents you will want to
// create an instance of the Order Structure, then use that to populate
// the vector of Orders that was passed in by reference.
// Once all of the contents are done being parsed the function will exit
// and your vector of Orders will have the appropriate data.
}
具有这种结构将使在解析文本文件时更易于调试。我相信从文件中检索所有数据并将其存储到某个容器中会更好一些。最容易使用的是string
或某些stream buffer
,例如stringstream
。完成获取所有内容后,关闭文件。从文本文件中读取所有内容并将其存储到字符串容器之后。这是您要解析strings
或streams
并检查信息是否有效的地方。多次打开和关闭文件句柄效率低下且速度慢,并且可能会出错。我认为从文件中获取内容,保存,关闭并完成文件操作,然后再进行实际工作会更容易。