3

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;
}
alios
  • 43
  • 9
  • 4
    1) `feof()` is always wrong http://stackoverflow.com/q/5431941/9059022) there is a '\n' at the end of each line that your program does not consume. – wildplasser Dec 19 '16 at 22:54
  • 2
    `"%c %c %d"` --> `" %c %c %d"` – chux - Reinstate Monica Dec 19 '16 at 23:01
  • 1
    Thanks, this quick trick works :) I suspected that something was wrong with the end of line, I tried the space after %d but it did not came to my mind putting it in the front :) – alios Dec 19 '16 at 23:21
  • 1
    To clarify why @chux's solution works: whitespace in a scanf format specifier means "ignore any whitespace before the match". The newline counts as whitespace. – Ray Dec 19 '16 at 23:29

1 Answers1

5

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");
};
Community
  • 1
  • 1
Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • Why `errno`? "Do what you want" could have been `strtol()` or some other function that sets `errno` that has nothing to do with file I/O. Further, `ret` could equal `EOF` and there is an error. few I/O functions specified by C set `errno`. Suggest `if (ret != EOF || ferror(transmissionsFile)) fprintf(...` – chux - Reinstate Monica Dec 19 '16 at 23:31
  • @chux Indeed I didn't see that the example in [man of scanf](https://linux.die.net/man/3/scanf). Do `errno = 0;` before call `scanf`. – Stargateur Dec 19 '16 at 23:35
  • 1
    I don't have soo much practice in C but I am improving, I'll keep in mind that... I am also checking the links, thanks for guidance :) – alios Dec 19 '16 at 23:38