C++ 如何从仅使用空格作为分隔符的文件中写入二维数组
我试图将一个文件放入3个不同的数组中。其中两个阵列是一维阵列,另一个是二维阵列。文本文件如下所示C++ 如何从仅使用空格作为分隔符的文件中写入二维数组,c++,arrays,file,C++,Arrays,File,我试图将一个文件放入3个不同的数组中。其中两个阵列是一维阵列,另一个是二维阵列。文本文件如下所示 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 loadOrd
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;
}
应该是什么时候
1 1 1 1 1
2 3 1 0 0
0 1 1 0 0
1 0 2 0 1
我将非常感谢您的帮助。您的问题是您在这里没有正确寻址2D阵列 例如,在
3x3
2D数组中,您有两个索引[a][b]
,该数组的2D表示形式如下所示:
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
#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.
}
例如,当您输出时:
for (int i = 0; i < 10; i++) {
cout << productsOrders[i][i] << endl;
}
for(int i=0;i<10;i++){
老实说,在解析文本文件时,我会根据其独特的职责将逻辑划分为单独的函数
您的代码将如下所示:
[0][0] [0][1] [0][2]
[1][0] [1][1] [1][2]
[2][0] [2][1] [2][2]
#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.
}
#包括
#包括
#包括
#包括
#包括
#包括
//数据类型的结构。。。
结构顺序{
std::stringfirstname;
std::字符串lastName;
std::vector productOrders;//可能使用vector而不是数组。。。
//您可能需要的任何其他变量或容器。。。
};
//简单的ostream运算符从文件内容中显示的信息;在内容之前是否有头信息?或者它是否从您显示的内容开始文件?我看到您有一个getline()
调用menuName
,然后从文件中提取prodCount
,最后在while循环中,在开始填充数组之前,您有一个fstream::get()
调用。只是尝试更好地理解文件结构,以尝试正确解析它。