I'm trying to write a piece of code that will read the "header" of a PPM file. For example:
P3
400 200
255
In this case the width is 400 and the height is 200 and the max colour value is 255. I'm trying to assign these strings values as integers but I think there is a better way to do this with less lines and more "safer." How can I avoid having to use the atoi() function? (Note I've already included the "check if file is open-able part in my ACTUAL code this is merely a reduced snippet)
char buffer[200];
char height[200];
char width[200];
char maxColour[200];
FILE *file = fopen("mcmaster.ppm", "r");
fgets(buffer, sizeof(buffer), file); // File format line
fgets(buffer, sizeof(buffer), file); // Width x height line
sscanf(buffer, "%s %s", width, height);
fgets(buffer, sizeof(buffer), file); // Max colour line
sscanf(buffer, "%s", maxColour);
int actHeight = atoi(height);
int actWidth = atoi(width);
int actMaxColour = atoi(maxColour);