ntroduction to C++ with Game Development: Part 17, File I/O, code not working

fdmfdm 101 Dec 28, 2012 at 20:40

hi there,
i have been trying to get the coding from part 17 of this tutorial working, but i keep getting this error:

1>c:\myprojects\template\devmaster_intro-to-c-tmpl83.00c_oct14\game.cpp(32): error C3861: ‘fget’: identifier not found

so after some searching on the internet i tries fgets instead of fget, giving me this error:

1>c:\myprojects\template\devmaster_intro-to-c-tmpl83.00c_oct14\game.cpp(33): error C2664: ‘fgets’ : cannot convert parameter 1 from ‘float’ to ‘char *’

i made the file settings.txt and put xpos = 100 in it, put it in the map with the source file (if you have seen the template, i put it in the same part as where the assets map is)

can anyone help me with this, and tell me what i did wrong?

and this is with the copied code of the tutorial btw , so there should be no typo’s

// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009

#include “string.h”
#include “surface.h”
#include “stdlib.h”
#include “template.h”
#include “game.h”

using namespace Tmpl8;

void Game::Init()


float x = 200, y = 0, vx = 0.1f, vy = 0;
void Game::Tick( float a_DT )
m_Screen->Clear( 0 );
m_Screen->Box( x, y, x + 5, y + 5, 0xffffff );
if ((vy += 0.02f, y += vy) > SCRHEIGHT) vy = -vy;
if ((x += vx < 0) || (x >= SCRWIDTH)) vx = -vx;

printf( “X-position: %f\nY-position: %f\n”, x, y );

FILE* f = fopen( “settings.txt”, “r” );
fget( f, “xpos = %f”, &x );
fclose( f );

2 Replies

Please log in or register to post a reply.

Reedbeta 167 Dec 28, 2012 at 21:49

If you have a line in your settings file that reads “xpos = 47.0” or some such, then probably what you want to read it is fscanf, which is more or less the inverse of fprintf. The function you’re trying to use, fgets, is just for reading a line as a string - it doesn’t do any kind of parsing or recognize numbers, etc.

fdmfdm 101 Dec 28, 2012 at 21:53

thanks that did it indeed :)

edit for future readers with the same problem:
to prevent the little box from bouncing on x=100 the entire time use:

if (read == false)
FILE* f = fopen( “settings.txt”, “r” );
fscanf( f, “xpos = %f”, &x );
fclose( f );
read = true;