Why didn't you use a class? Managing products across different arrays like that is not good at all. What's worse is that there are better containers to be used too for expandable data in C++. This array method you're using would be more of a C-like strategy. You are also not closing the stream, or checking if it is open...
Ex:
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
class Product
{
public:
std::string Name;
std::string Quantity;
std::string Prize;
std::string Code;
};
std::ostream& operator<<(std::ostream& o, const Product& obj)
{
o << "Name: " << obj.Name << '\n'
<< "Quantity: " << obj.Quantity << '\n'
<< "Prize: " << obj.Prize << '\n'
<< "Code: " << obj.Code << std::endl;
return o;
}
int main()
{
std::vector<Product> products;
std::ifstream fs("data.txt");
if (!fs.is_open()) return 1;
std::string ln;
while (getline(fs,ln))
{
if (std::count(ln.begin(), ln.end(), '|') == 3)
{
Product p;
size_t i = 0;
size_t index(ln.find('|', 0));
p.Name = ln.substr(i, index);
i = index + 1;
index = ln.find('|', i);
p.Quantity = ln.substr(i, index - i);
i = index + 1;
index = ln.find('|', i);
p.Prize = ln.substr(i, index - i);
i = index + 1;
index = ln.find('|', i);
p.Code = ln.substr(i, index - i);
products.push_back(p);
}
}
fs.close();
std::for_each(products.begin(), products.end(), [](Product p) { std::cout << p << std::endl; });
}
Quick and dirty implementation. Tested on a file that looks like this:
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Product #1|Quantity|Prize|Code
Product #2|Quantity|Prize|Code
Output:
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #1
Quantity: Quantity
Prize: Prize
Code: Code
Name: Product #2
Quantity: Quantity
Prize: Prize
Code: Code
What would be even better is to use a std::map, and use a product ID (if there is one, I'm assuming it may be "Code"), as the link to the mapped value. This way, it would be much easier to find a Product once all the data is retrieved from the file. IMHO, even if you just choose a vector, it's much better than these arrays you're using...