Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<Cliente> readclientetxt();
- // files reading
- vector<Cliente> clientes = readclientetxt();
- vector<Cliente> readclientetxt() //save the file clientes.txt in one vector of clientes (int ; string ; double)
- {
- vector<Cliente> clienteslista;//vector that is returned in the end (which is going to have Clients.txt info)
- ifstream fichClientes;//ifstream to store the text file, and then extract the information from it
- fichClientes.open("clientes.txt");//store the text file in the ifstream
- if (!fichClientes)//error message in case of non-existence of the file
- cerr << "ERROR 404 - Clients file not found";
- while (!fichClientes.eof())//cicle to create the vector (ends in end of file)
- {
- Cliente clienteauxdata(fichClientes);//create the cliente which is going to store the info of one line
- string clienteinfo; //string to store the line
- int numCliRead; // number of clients to read
- string numClientesR;
- getline(fichClientes, numClientesR); // Define conditions if a user don't digit a number
- numCliRead = stoi(numClientesR, nullptr, 10);
- while (numCliRead)
- {
- getline(fichClientes, clienteinfo);//store the line in clientinfo, next time its called, reads the next line
- string idRead;//string which stores id information
- string nameRead;//string which stores name information
- string amountRead; //string which stores amount information
- int stringcounter = 0; //store string character place
- for (int i = 0; clienteinfo[i] != ' '; i++)//cicle to read id , lê até encontrar o primeiro espaço char(20) é o espaço!
- {
- idRead = idRead + clienteinfo[i];
- stringcounter = i;
- }
- for (int k = stringcounter + 3; clienteinfo[k] != ';'; k++)//cicle to read name
- {
- nameRead = nameRead + clienteinfo[k];
- stringcounter = k;
- }
- nameRead = nameRead.substr(1, nameRead.size() - 2);
- for (int j = stringcounter + 2; clienteinfo[j]; j++)//cicle to read amount
- {
- amountRead = amountRead + clienteinfo[j];
- }
- clienteauxdata.setId(idRead);//convert string to int
- clienteauxdata.setNome(nameRead);
- clienteauxdata.setVolCompras(amountRead);//convert string to double
- //create the client
- clienteslista.push_back(clienteauxdata);//store client in vector next line
- }
- }
- return clienteslista;//return the vector
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement