Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22001

Is it possible to have two fgets() as a condition of a while?

$
0
0

I'm working on a CRUD. II have to do it in standar ansi89. But the first, is to do a parser of a flat file(fixed width). I have 3 files, the inventory, the new items and a temporal one where i am going to write the new inventory and later rename it.

The problem: after I opened the tree files, I try to enter in a while loop which read from inventory.txt and new.txt with fgets(). But the program doesn't enter in the loop. I know that it stops just before there because I put message for all the code looking for the mistake. When I execute the program, its behaviour is ok, it doesn't gives me an error, just doesn't work.

I tried putting a do{}while(); and then it runs just a time.

if((ft = fopen(PATH_TEMPORAL_FILE, "wt")) == NULL)
    {
        puts(MSG_ERROR_TEMPORAL_FILE);
        return ERROR_TEMPORAL_FILE;
    }   
    if((fa = fopen(PATH_UP_FILE, "wt")) == NULL)
    {
        puts(MSG_ERROR_UP_FILE);
        return ERROR_UP_FILE;
    }   
    if((fi = fopen(PATH_INVENTORY_FILE, "wt")) == NULL)
    {
        puts(MSG_ERROR_INVENTORY_FILE);
        return ERROR_INVENTORY_FILE;
    }   

    while((n1 = fgets(line_one,MAX_INPUT_LINE + 2,fi)) != NULL || (n2 = fgets(line_two,MAX_INPUT_LINE + 2,fa)) != NULL)
    {
        memcpy(aux_one, line_one + FIELD_ID_POSITION, FIELD_ID_WIDTH);
        aux_one[FIELD_ID_WIDTH] = '\0';
        r1.id = (size_t)strtoul(aux_one, &temp, 10);
        if(*temp && *temp != '\n'){
            fprintf(stderr, "Error %c\n", *temp);
            return ERROR_INPUT_FILE;
        }

        memcpy(aux_two, line_two + FIELD_ID_POSITION, FIELD_ID_WIDTH);
        aux_two[FIELD_ID_WIDTH] = '\0';
        r2.id = (size_t)strtoul(aux_two, &temp, 10);
        if(*temp && *temp != '\n'){
            fprintf(stderr, "Error %c\n", *temp);
            return ERROR_INPUT_FILE;
        }

        memcpy(aux_three, line_one + FIELD_RESOURCE_POSITION, FIELD_RESOURCE_WIDTH);
        aux_three[FIELD_RESOURCE_WIDTH] = '\0';
        rsrc1 = &aux_three;

        memcpy(aux_four, line_two + FIELD_RESOURCE_POSITION, FIELD_RESOURCE_WIDTH);
        aux_four[FIELD_RESOURCE_WIDTH] = '\0';
        rsrc2 = &aux_four;      

        memcpy(aux_five, line_one + FIELD_TRAFFIC_POSITION, FIELD_TRAFFIC_WIDTH);
        aux_five[FIELD_RESOURCE_WIDTH] = '\0';
        r1.traffic = (float)strtoul(aux_five, &temp, 10);
        if(*temp && *temp != '\n'){
            fprintf(stderr, "Error %c\n", *temp);
            return ERROR_INPUT_FILE;
        }

        memcpy(aux_six, line_two + FIELD_TRAFFIC_POSITION, FIELD_TRAFFIC_WIDTH);
        aux_six[FIELD_TRAFFIC_WIDTH] = '\0';
        r2.traffic = (float)strtoul(aux_six, &temp, 10);
        if(*temp && *temp != '\n'){
            fprintf(stderr, "Error %c\n", *temp);
            return ERROR_INPUT_FILE;
        }

        if(r1.id < r2.id){
            fprintf(ft,"%lu", r1.id);

            for(i = 0; i < FIELD_RESOURCE_WIDTH; i++){
                fputs(rsrc1 -> resource, ft);
                rsrc1 += i;
            }

            fprintf(ft,"%f",r1.traffic);
        }
        else
        {
            fprintf(ft,"%lu", r2.id);

            for(i = 0; i < FIELD_RESOURCE_WIDTH; i++){
                fputs(rsrc2 -> resource, ft);
                rsrc2 += i;
            }

            fprintf(ft,"%f",r2.traffic);        
        }

        while((n1 != NULL) && (n2 == NULL)) 
        {
            fprintf(ft,"%lu", r1.id);

            for(i = 0; i < FIELD_RESOURCE_WIDTH; i++){
                fputs(rsrc1 -> resource, ft);
                rsrc1 += i;
            }

            fprintf(ft,"%f",r1.traffic);
        }

        while((n2 != NULL) && (n1 == NULL))
        {
            fprintf(ft,"%lu", r2.id);

            for(i = 0; i < FIELD_RESOURCE_WIDTH; i++){
                fputs(rsrc2 -> resource, ft);
                rsrc2 += i;
            }

            fprintf(ft,"%f",r2.traffic);
        }   
    }

Viewing all articles
Browse latest Browse all 22001

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>