Read line of text file with fixed format

0

I'm not sure which is the best way to read The following text file which was generated in the following way:

Struct
int nro_orden;
char desc[30];
int cantidad;





fprintf(pf,"%03d%-30s%03d\n",pedido[i].nro_orden,pedido[i].desc,pedido[i].cantidad);

001Alpargatas                    050

002Zapatillas Nike               020

003Calzones                      040

004Medias levis                  025

005Chomba Adidas                 030

I am reading the file in the following way:

char string[38];

while(fgets(string,sizeof(string),pf))
{
    sscanf(string,"%03d%29[^\n]%03d",&pedido[i].nro_orden,pedido[i].desc,&pedido[i].cantidad);

}

But the problem is that I request [i] .desc I have the description + the blanks. I do not know if there is a way to get this description without them or if I have to get them after obtaining them.

Thank you very much for your time

    
asked by Clown 123 16.07.2018 в 00:12
source

2 answers

0

When you read strings in Language C with scanf the %s format only reads up to the first blank, another format is %[regex] where regex is a regular expression where you will read scanf if that expression is true.

Therefore:

%[^\n] specifies that it will read until the entry is a line break
%[^0-9] specifies that it will read until the entry is a digit of 0 a 9
%[regex] any other expression you can think of

#include <stdio.h>

typedef struct
{
    int nro_orden;
    char desc[30];
    int cantidad;
} pedido_t;

int main()
{
    // Escribiendo los datos
    {
        FILE *file = fopen("file.txt", "w");
        pedido_t pedido[5] = 
        {
            { 1, "Alpargatas",      50 },
            { 2, "Zapatillas Nike", 20 },
            { 3, "Calzones",        40 },
            { 4, "Medias levis",    25 },
            { 5, "Chomba Adidas",   30 }
        };

        for(int i = 0; i < 5; i++)
        {
            fprintf(file, "%03d%-30s%03d\n",
                pedido[i].nro_orden,
                pedido[i].desc,
                pedido[i].cantidad);
        }

        fclose(file);
    }

    // Leyendo los datos
    {
        FILE *file = fopen("file.txt", "r");
        pedido_t pedido;

        while(!feof(file))
        {
            fscanf(file, "%d%[^0-9]%d", 
                &pedido.nro_orden,
                 pedido.desc,
                &pedido.cantidad);

            printf("%03d %-30s %03d\n",
                pedido.nro_orden,
                pedido.desc,
                pedido.cantidad);
        }

        fclose(file);
    }

    puts("\n");
    return 0;
}
    
answered by 17.07.2018 / 22:36
source
0

The solution to the answer above is correct. Unless the spaces are stored unnecessarily for some cases. The only thing I would add would be a function that removes spaces from more than one string of characters.

One way to do this function would be:

void eliminar_espacios(char cad[]){
  int i=strlen(cad)-1;
    while(cad[i]==' '){
        cad[i]='
void eliminar_espacios(char cad[]){
  int i=strlen(cad)-1;
    while(cad[i]==' '){
        cad[i]='%pre%';
        i--;
    }
}
'; i--; } }

I hope it serves you, any questions ask me without problems

    
answered by 30.07.2018 в 04:32