#How to read a txt file ?

12 messages · Page 1 of 1 (latest)

vapid portal
#

Here is my function that read a name and a score that is send to a struct but my programm stop at the moment I go there. Why ?
Here is my code
void read_scores(char* filename, struct Player* P) {
printf("Opening file: %s\n", filename);
FILE * file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
P->score = 0;
return;
}

char name[MAX_NAME_LENGTH];
int score;
while (fscanf(file, "%s %d", name, &score) != EOF) {
    if (strcmp(name, P->name) == 0) {
        P->score = score;
        break;
    }
}
fclose(file);

}

compact nimbusBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

compact nimbusBOT
#

Here is my function that read a name and a score that is send to a ```cpp
struct but my programm stop at the moment I go there.Why
? Here is my code void
read_scores(char* filename, struct Player* P) {
printf("Opening file: %s\n", filename);
FILE* file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
P->score = 0;
return;
}

char name[MAX_NAME_LENGTH];
int score;
while (fscanf(file, "%s %d", name, &score) != EOF) {
if (strcmp(name, P->name) == 0) {
P->score = score;
break;
}
}
fclose(file);
}

skavengers
verbal abyss
#

!f

compact nimbusBOT
#
void read_scores(char* filename, struct Player* P) {
  printf("Opening file: %s\n", filename);
  FILE* file = fopen(filename, "r");
  if (file == NULL) {
    printf("Could not open file %s\n", filename);
    P->score = 0;
    return;
  }

  char name[MAX_NAME_LENGTH];
  int score;
  while (fscanf(file, "%s %d", name, &score) != EOF) {
    if (strcmp(name, P->name) == 0) {
      P->score = score;
      break;
    }
  }
  fclose(file);
}
Monke
verbal abyss
vapid portal
#

I received this error

#

So the txt file isn’t printed when it should

#

here is how I call it

#

int main(){
read_scores('player_scores.txt',&P[i]);
return 0;
}

vapid portal
#

!solved