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
127 views
in Technique[技术] by (71.8m points)

c - How to convert specific letters from a file and print to an array?

struct reviewStruct {
  char reviewer[50];
  int feedback[3];
};

int readReviews(FILE *file, struct reviewStruct reviews[10]) {

  int i;
  file = fopen("Names.txt", "r");
  if(file == NULL) {
      printf("Error");
      exit(-1);
  }
  for(i = 0; i < 10; i++) {
    fgets(reviews[i].reviewer, 50, file);
  }
  
  fclose(file);

  for(i = 0; i < 10; i++) {
    printf("%s", reviews[i].reviewer);
  }
  return 0;
}

Hello, I'm trying to read a file line by line and print it to an array, with a catch. Whenever a 'Y' or 'y' appears, it converts that letter into a 1, and if an 'N' or 'n' appears, it is converted into a 0 (zero), excluding the first word of every line. For example, I have a file with the following information:

charlie Y n N
priya N n Y
lance y y Y
stan N y n
arin N n N

This is the text file called Names.txt, I want to save the integer information to the array called "feedback", so that it looks like this when printed using a for loop:

1 0 0
0 0 1
1 1 1
0 1 0
0 0 0

How do I populate the feedback array such that it can be printed along with the names using a for loop as it is in the following image?

charlie 1 0 0
priya 0 0 1
lance 1 1 1
stan 0 1 0
arin 0 0 0

Thanks.

question from:https://stackoverflow.com/questions/65928937/how-to-convert-specific-letters-from-a-file-and-print-to-an-array

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

...