Using fgets to read strings from file in C

前端 未结 4 1210
再見小時候
再見小時候 2021-01-22 13:07

I am trying to read strings from a file that has each string on a new line but I think it reads a newline character once instead of a string and I don\'t know why. If I\'m going

4条回答
  •  情话喂你
    2021-01-22 13:51

    if Enimi/Pnimi/Kood are arrays not pointers:

    while( fgets(inimene[i].Enimi,sizeof inimene[i].Enimi,F1) &&
           fgets(inimene[i].Pnimi,sizeof inimene[i].Pnimi,F1) &&
           fgets(inimene[i].Kood,sizeof inimene[i].Kood,F1) )
    {
    if( strchr(inimene[i].Enimi,'\n') ) *strchr(inimene[i].Enimi,'\n')=0;
    if( strchr(inimene[i].Pnimi,'\n') ) *strchr(inimene[i].Pnimi,'\n')=0;
    if( strchr(inimene[i].Kood,'\n') ) *strchr(inimene[i].Kood,'\n')=0;
    printf("i=%d\nEnimi=%s\nPnimi=%s\nKaad=%s", i, inimene[i].Enimi, inimene[i].Pnimi,inimene[i].Kood);
        i++;
    }
    

提交回复
热议问题