Skip to main content
GameDev.net gamedev.net
🔒 Locked

General programming problem - please help

Started by csxpcm May 24, 2003 at 10:46 AM 3 replies 250+ views
Original Post
csxpcm
csxpcm
Hi Everyone, The code shown below just wont work, and I can't figure out why. It just seems to output unitialised characters when I do a COUT to check if it's working. Has anyone got any ideas that I might try? It's driving me crazy. The file I'm reading from looks like this: NAMES 3 Stuart,Fred,Jones Steve,Jack,Brown Michael,Paul,Jeffrey
    

  #include <fstream.h>
  #include "stdafx.h"
  #include <iostream.h>

typedef struct
{
  char firstname;
  char middlename;
  char Surname;
} NAME;

void readstr(FILE *f,char *string)
{
  do
  {
     fgets(string, 255, f);
  } while ((string[0] == '/') || (string[0] == '\n'));
  return;
}



int _tmain(int argc, _TCHAR* argv[])
{
  int numNAMES;
  FILE *filein;
  char oneline[255];
	
  filein = fopen("data/names.txt", "rt");  // File To Load World Data From


  readstr(filein,oneline);
  sscanf(oneline, "NAMES %d\n", &numNAMES);

  cout << numNAMES << endl;    // Works OK!!


  NAME* names2;
  names2 = new NAME[numNAMES];   // dynamically allocate memory


  char a,b,c;
  int  numberOfNames = numNAMES;  // Update the variable


  for (int loop=0; loop<numberOfNames; loop++)
  {
    readstr(filein, oneline);
    sscanf(oneline, "%f,%f,%f", &a, &b, &c);
    names2[loop].firstname   = a;  //1

    names2[loop].middlename  = b;  //2

    names2[loop].Surname     = c;  //3

  }

  cout << names2[2].firstname;

  fclose(filein);
  return 0;
}







    
[edited by - csxpcm on May 24, 2003 11:51:13 AM]
csxpcm
csxpcm
Nope- didnt work
But Thanks anyway!!

What would you recommend to read input, in C++ style?

kind regards.
foofightr
foofightr
#include <fstream.h>
#include "stdafx.h"
#include <iostream.h>

Assuming you use VC++, it will silently ignore anything that comes before the #include "stdafx.h"

The lesson is always include stdafx.h before anything else.

[edited by - foofightr on May 24, 2003 12:00:33 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.