Hi Guys,

I need some advice on reading an integer from a file.
The file would always have one integer. Once the integer is
read in, I would use this in 'if else' statement to find the outcome

f = fopen (fileName, "r");
	if (f == NULL)
		return 0;
	fread();
	fclose (f);

Can I use fread to perform this task?

Thanks

Can I use fread to perform this task?

Yes, but I suspect fscanf is what you want:

int value;

if (fscanf(f, "%d", &value) == 1) {
    /* Successfully read an int value */
}
commented: Quick reply and excellent advice +3

Yes, but I suspect fscanf is what you want:

int value;

if (fscanf(f, "%d", &value) == 1) {
    /* Successfully read an int value */
}

Sorry for the late reply, but your suggestion worked perfectly.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.