Step 5 of 5
Read file
std::ifstream + getline reads line by line. Then parse each line to rebuild the struct. Continue with inventory and file tutorials.
Your file so far
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
struct Producto {
int id;
std::string nombre;
double precio;
};
int main() {
std::vector<Producto> inventario = {
{1, "Teclado", 25.50},
{2, "Mouse", 12.00},
};
std::ofstream out("inventario.txt");
for (const auto& p : inventario) {
out << p.id << ';' << p.nombre << ';' << p.precio << '\n';
}
out.close();
std::ifstream in("inventario.txt");
std::string linea;
while (std::getline(in, linea)) {
std::cout << linea << std::endl;
}
return 0;
}