Mail Archives: djgpp/1999/05/16/17:55:26
>I'm making a program which needs to read a mixture of number and words
>from a text file. For words, I'd use:
> char name[10];
>or something like that, to be able to store a word up to 10 characters
>long, and read data from the file into this ("dataFile >> name;"). I
>also use:
> int number;
>to be able to read a number. However, when reading more than one lot of
>data, I could use:
> int number[5];
>but what would I do for 'name', and how would I read data into it from a
>file?
If the number of strings is always the same, you could simply try a struct
with the appropriate number of strings, numbers, etc. I.e., you'd make a
database:
typedef struct {
char myFirstString[10];
char mySecondString[10];
short myFirstShort;
long myFirstLong;
} myRecord;
etc.
Otherwise, you'd have to divse a way of letting your program know what to
expect from the file (by using a header in the file, or a pattern check
from your data stream). For example, if after the word "Name", a string of
ten characters is always expected, you'd scan your input until you came up
with the word "Name" and take the next ten characters to be a name using
#include <stdio.h>
char name[10]
bool rc = true;
rc = (fread(&name, 10, 1, myFile) == 1);
(rc will be true if this was a success, don't forget to define bool)
"name" will then be filled with those ten characters.
Hope I've been of some assistance,
Dlanor
- Raw text -