jeudi 9 juillet 2020

I'm trying to extract data from a csv file but I can't exactly get it right

This is what my code should do:

  1. Take a reading with a barcode scanner, it consists of 16-17 string depending on the color palette type.
  2. I segment what i read into smaller parts to process, A, B1, B2, C (the substr works perfectly fine)
  3. I take those substrings and process them into a readable format.

The problem is that I can't (I don't know how to) extract the data correctly so that B2 can be compared to its corresponding colour code and for colorname to be displayed correctly.

Here's a sample of the csv file I'm trying to extract the data from, it consists of multiple rows and two columns:

RAL7016,NEGRO RAL7017,BLANCO RAL7018,AZUL RAL7019,VERDE YW207F,AZULOSCURO Y2208I,BEIGE Y4304I,AMARILLO YX353F,CIELO Y2212I,TURQUESA

First row can contain 6 or 7 characters which is why I ask in the code if RAL is present.

If anyone can help me I would really appreciate it. Many thanks.

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

main()
{
    string scan, A, B1, B2, C;
    
    Init:
    int x=0, y=0;

    cout << "\nEsperando a scan...";
        
    cin >> scan;
    cout << "Codigo:" << scan;
    
    A = scan.substr (0,3); 
    B1 = scan.substr (3,3); 
    B2 = scan.substr (6,3);
    if (B2 == "RAL") B2 = scan.substr(5,7), C = scan.substr (13,4);
    else B2 = scan.substr(5,6), C = scan.substr (12,4);
    
    //Tipo de producto
    if (A == "PUE") A = "PUERTA", x=1;
    if (A == "PAN") A = "PANEL", x=1;
    if (A == "LAC") A = "LACADO", x=1;
    if (x=0) A = "PRODUCTO NO AGREGADO AL SISTEMA";
    
    //Tipo de acabado
    if (B1 == "BRI") B1 = "BRILLO", x=1;
    if (B1 == "TEX") B1 = "TEXTURADO", x=1;
    if (B1 == "MAT") B1 = "MATE", x=1;
    if (x=0) B1 = "TIPO DE ACABADO NO AGREGADO AL SISTEMA";
        
    //Leer colour desde archivo csv
    string colourcode = "blank", colourname = "blank";
    
    ifstream myfile("colourtable.csv");
    
    if(myfile.is_open()){
        while(getline(myfile, colourcode, ',') && getline(myfile, colourname)){
    
            if(B2 == colourcode){
                if(B2.substr (0,3) == "RAL"){
                    B2 = colourname.substr(8,20);
                }
                else B2 = colourname.substr(7,20);
            }
        }
    myfile.close();
    }
    else B2 = "Falta archivo colourtable.csv"; 
  
    //Output en pantalla
    cout << "\n Producto:" << A << "\n Acabado:" << B1 << "\n colour:" << B2 << "\n N Pedido:" << C << "\n" << "Y = "<< y << "\n";
    
    //colour debug
    cout << "\n colourcode:" << colourcode << "\n colourname:" << colourname;
    //Output en archivo a pedidos.csv
    fstream file;
    
    file.open ("pedidos.csv");
    
    if (file.is_open()){
        file << C << "," << B2 << "," << B1;
        file.close();
    }
    else cout << "\n Falta archivo pedidos.csv";
    //Reiniciar ciclo
    goto Init;
}

Aucun commentaire:

Enregistrer un commentaire