Original Post
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
[edited by - csxpcm on May 24, 2003 11:51:13 AM]
#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;
}