Inputing from a file

moonshot used Ask the Experts™
I'm writing a simple rpg game.  I'm trying to load monster and item data from a file, and visual c++ isn't liking me too much.  Below is my code and the errors I'm receiving.  I'm using VC++ 5.0 and it's a console app.

#include <afx.h>
#include <fstream.h>

int main()

     ifstream f_monsters("monsters.txt");

     return EXIT_SUCCESS;

nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/fileloader.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

Also, what is the easiest way to read in data?  I was gonna format my file something like...:

Orc: 30 32 120 30 60

... and just read up to the colon for the name, then grab the ints for the other stats.  Is there a better way?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Go to the project settings(menu: Project->Settings)
Under the General Tab. In the combo box for Microsoft foundantion classes, select the option 'use MFC in a shared Dll' then press ok and compile and link.

This should fix your problem. Another solution would be that in the project settions, under the C/C++ tab, under the Catgeory choose Code Generation, then Set ‘Use run-time library’ to ‘Debug Multithreaded’.

The first solution will remove your problem. The second solution might present other problems which you need to post here for me to tell you what to do next.
Anyway, you can format your file anyway you want and read it that way. The method you proposed is good, and you don't even need the colon.

Here's some code to get you started:

#include <stdio.h>

int main()

  FILE *file;
  /* This will store the monster's name and the numbers.
     I don't know what the numbers are for so I
     interpreted them as stats. You might want to use an
     array of these if you have multiple characters */
  char monster[10];
  int health, magic, power, stamina, iq;

  file = fopen("monsters.txt", "r");

    printf("Error: can't open file.\n");
    return 1;

     /* This will read a line and store it into the variables..
        Remember to change the variables if you are
        reading more than one line (i.e. increment your
        array counter ->  monster[1] .. monster[2] etc */

    fscanf(file, "%s %d %d %d %d %d", &monster, &health, &magic, &power, &stamina, &iq );

/* Check the values you got.. */
 printf("%s %d %d %d %d %d", monster, health, magic, power, stamina, iq );

    return 0;
I would strongly suggest that you do it the way kooheji has sugested. thats the way i read data file into all programs i develop.


Excellent answer, thanks a lot.  I actually ended up using ifstream.  Most of the names of my monsters are preceeded with "A", "An", or "The."  It's all working now though, thanks again.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial