alios
alios

Reputation: 43

A weird issue with reading from file

I am working on a homework and I am stuck with this problem of some part of my code working beyond my understanding. I have searched many forums and tried some other approaches to overcome the problem yet I could not find a solution. This is the reduced problematic part of my code...

    while(!feof(transmissionsFile)){
        int ret = fscanf(transmissionsFile, "%c %c %d", &tTms.tsn, &tTms.rsn, &tTms.sTime);
        if(ret != 3) continue;

        for(s = 0; s < N; ++s){
            if(tmsMtx[s][tTms.sTime].tsn == ' ' ){
                tmsMtx[s][tTms.sTime] = tTms;
                break;
            }
            continue;
        }       
    }

the file that I am reading is a simple txt file:

A B 3
C B 2
C A 1
B A 2
A C 3
B C 4

but when I tried to read the file, it only reads

A B 3
C A 1
A C 3

I have checked the returning values of "ret" function and I see that it returns 3 for the ones read, twice times 2 for the ones unread... I also am using the exactly same format for reading data into some other structure perfectly... There must be some thing that I am missing but I can't figure it out. I would appreciate any guidance :)

This is the whole code I wrote to solve this specific problem, for the ones who might wonder, this part should create a transitions matrix and read the values to fill the matrix. Ex: A to B in T3 for the first line of file...

#include <stdio.h>

int main()  {

    int N = 5;
    //  Opening required files  ############################################    
        FILE *transmissionsFile = fopen("transmissions.txt", "r");
    if ( transmissionsFile == 0 ) {
        printf( "\nCould not open transmissions.txt\n" );
        return 1;
    }   

    //  Structure definitions   ############################################
    typedef struct transmissions {
        char tsn;
        char rsn;
        int sTime;
    } transmission;

    //  Constructing the transissions matrix    ############################
    transmission tTms = {' ', ' ', 0};
    transmission tmsMtx[N][2*N];
    int s,t;
    for(s =0; s < N; s++){
        for(t =0; t < 2*N; t++){
            tmsMtx[s][t] = tTms;
        }
    }

    while(!feof(transmissionsFile)){
        int ret = fscanf(transmissionsFile, "%c %c %d", &tTms.tsn, &tTms.rsn, &tTms.sTime);
        if(ret != 3) continue;

        for(s = 0; s < N; ++s){
            if(tmsMtx[s][tTms.sTime].tsn == ' ' ){
                tmsMtx[s][tTms.sTime] = tTms;
                break;
            }
            continue;
        }       
    }

    //  Print out the transmission matrix   ################################
    system("cls");
    printf ("\n  The transmission matrix \n\n  ");
    for(t =0; t < 2*N; t++){
            printf(" T%d\t", t);
    }
    printf("\n\n");
    for(s =0; s < N; s++){
        printf("  ");
        for(t =0; t < 2*N; t++){
            printf("%c-%c\t", tmsMtx[s][t].tsn, tmsMtx[s][t].rsn);
        }
        printf("\n\n");
    }

    //  Closing files and ending program    ################################    
    fclose(transmissionsFile);

    return 0;
}

Upvotes: 3

Views: 106

Answers (1)

Stargateur
Stargateur

Reputation: 26697

Like @chux and @wildplasser say. It's because you don't parse the new line with scanf:

int ret = fscanf(transmissionsFile, "%c %c %d\n", &tTms.tsn, &tTms.rsn, &tTms.sTime);
if (ret != 3) {
 fprintf(stderr, "something wrong !!!\n");
 continue;
};

Like @wildplasser say. You should not use feof() like that Why is “while ( !feof (file) )” always wrong?.

int ret;
while ((ret = fscanf(transmissionsFile, "%c %c %d\n", &tTms.tsn, &tTms.rsn,
                     &tTms.sTime)) == 3) {
  // Do what you want
}
if (ret != EOF || ferror(transmissionsFile)) {
  fprintf(stderr, "something wrong !!!\n");
};

Upvotes: 5

Related Questions