Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

c - fscanf not reading file correctly

Helllo I was hoping to get some help, my function is not populating an array correctly, I have to take in a name a sting and then convert y or n to a 1 or 0 and store it. my function returns this:

Ritu010
N010
Larry000
n010
Dan010
y010
Judi000
y000
Eric010
y000

when it should be something like this:

Ritu Y N y
Larry n n y
Dan y y y
Judi n y n
Eric y y n
Isabelle y n y
Manisha y n n
Terra Y Y N
Dora n y n
Nick n n y

Code:

int readReviews (FILE * fptr, struct reviewStruct r[NUMBER_REVIEWERS]){
    char ch;
    int i=0;
    while(!feof(fptr)){
          fscanf(fptr,"%s",r[i].reviewer);
          for (int j= 0; j < NUMBER_MOVIES; j++){
              fscanf(fptr,"%c",&ch);
              if (ch == 'y') {              
                r[i].feedback[j] = 1;
              }
              else if(ch=='Y'){
                r[i].feedback[j] = 1;
              }
              else if(ch=='n'){
                r[i].feedback[j] = 0;
              }
              else if (ch=='N'){
                r[i].feedback[j] = 0;
              }
           }
        i++;
    }
    return 1;
}
question from:https://stackoverflow.com/questions/65947363/fscanf-not-reading-file-correctly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[4] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...