Taller 3 - Archivos Binarios

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

#include <iostream>

#include <fstream>
#include <string>
#include <cstring>
#include <sstream>

using namespace std;

struct Tool{
int id;
char name[20];
int quantity;
int cost;
};

struct Tools{
int size;
Tool tools[100];
}tool;

void readFile(string nameFile, Tools &tool, char delimiter){


fstream myFile;
string line, word;
int count = 0;
Tool aux_tool;

myFile.open(nameFile, ios::in);

if (!myFile.is_open()){
cout << "Error while opening file " << nameFile << endl;
}

while(getline(myFile, line)){
stringstream str(line);
getline(str, word,delimiter);
aux_tool.id = stoi(word);
getline(str, word,delimiter);
strcpy(aux_tool.name, word.c_str());
getline(str, word,delimiter);
aux_tool.quantity = stoi(word);
getline(str, word,delimiter);
aux_tool.cost = stoi(word);

tool.tools[count] = aux_tool;
count++;
}

myFile.close();
tool.size = count;
};

fstream myFile;
string nameTxt = "herram.txt", nameBin = "herram.bin", line;
char delimiter = ' ';
int main() {

int option;

while (option != -1){


cout << "\nWhat do you want to do?" << endl;
cout << "\nOption 1 = capture data" << endl;
cout << "Option 2 = data list" << endl;
cout << "Option 3 = sort the file" << endl;
cout << "Option 4 = insert a tool manually" << endl;
cout << "Option 5 = modify the data of a tool" << endl;
cout << "Option 6 = delete a tool and update file\n" << endl;
cout << "Insert -1 if you want to end the code" << endl;
cout << "\nOption: ";
cin >> option;
cout << "\n";

switch (option){
case 1:{
readFile(nameTxt, tool, delimiter);

break;
}

case 2:{
for (int i = 0; i < tool.size; i++){
if (tool.tools[i].cost != 0){
cout << tool.tools[i].id << " " << tool.tools[i].name << " "
<< tool.tools[i].quantity << " " << tool.tools[i].cost << endl;
}
}

if (tool.tools[tool.size].cost != 0 &&
tool.tools[tool.size].quantity !=0){
cout << tool.tools[tool.size].id << " " <<
tool.tools[tool.size].quantity << " " << tool.tools[tool.size].cost << "
" << tool.tools[tool.size].name << endl;
}

break;
}

case 3:{
Tool aux_tool;

if (tool.tools[tool.size].quantity && tool.tools[tool.size].cost


!= 0){
tool.size = tool.size + 1;
}

for (int i = 0; i < tool.size - 1; i++){


for(int j = 1; i < tool.size; j++){
if(tool.tools[j].id > tool.tools[i].id){
aux_tool = tool.tools[i];
tool.tools[i] = tool.tools[j];
tool.tools[j] = aux_tool;
}
}
}

break;
}

case 4:{
int provI, provQ, provC; //ID, quantity and cost provisional
char provN[20];

cout << "\nTool ID: ";


cin >> provI;
cout << "Tool name: ";
cin >> provN;
cout << "Tool quantity: ";
cin >> provQ;
cout<<"Tool cost: ";
cin >> provC;

tool.tools[tool.size].id = provI;
for (int i = 0; i < 20; i++){
tool.tools[tool.size].name[i] = provN[i];
}
tool.tools[tool.size].quantity = provQ;
tool.tools[tool.size].cost = provC;

break;
}

case 5:{
int provI, provQ, provC; //ID, quantity, cost
char provN[20]; //and name provisional

cout << "Enter the ID of the tool to modify" << endl;


cin >> provI;

cout << "\nEnter the new data: " << endl;


cout << "Tool name: ";
cin >> provN;
cout << endl;
cout << "Tool quantity: ";
cin >> provQ;
cout << endl;
cout << "Tool cost: ";
cin >> provC;
cout << endl;

for (int i = 0; i < tool.size; i++){


if (provI == tool.tools[tool.size].id){
tool.tools[i].quantity = provQ;
tool.tools[i].cost = provC;
for (int j = 0; j < 20; i++){
tool.tools[tool.size].name[i] = provN[i];
}
}
}

break;
}

case 6:{
Tool aux_tool;
int provI;

cout << "Enter the ID of the tool to remove" << endl;


cin >> provI;

for (int i = 0; i < tool.size; i++){


if (provI == tool.tools[tool.size].id){
tool.tools[tool.size].id = 0;
for (int j = 0; j < 20; i++){
tool.tools[tool.size].name[i] = ' ';
}
tool.tools[tool.size].quantity = 0;
tool.tools[tool.size].cost = 0;

}
}

break;
}
}
}
}

You might also like