Auram
Auram

Reputation: 19

Get the file text into a variable in C

I am doing a game and I want to do a highscore save with a file.

I have a highscore.txt that has written in it '55'

I want to get that 55 number into a variable. Can you guys help me.

I only can put one int in my variable with my code and don't know how to do a loop as I use ints and chars :

char c;
c = fgetc(fichier) - '0';

Thanks.

Upvotes: 0

Views: 26

Answers (1)

bobra
bobra

Reputation: 625

For your issues fscanf/fprintf will be more sufficient http://www.cplusplus.com/reference/cstdio/fscanf/

#include <stdio.h>

int main(){
    int score;
    FILE* f = fopen("highscore.txt", "r");
    fscanf(f, "%d", &score);
    return 0;
}

Upvotes: 1

Related Questions