www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/05/16/17:55:26

X-Sender: dlanor AT mail DOT dds DOT nl
Message-Id: <l03130303b364ea4cb508@[145.98.116.109]>
In-Reply-To: <LOzMxDAi+sP3EwcQ@zaynar.demon.co.uk>
Mime-Version: 1.0
Date: Sun, 16 May 1999 23:39:40 +0200
To: djgpp AT delorie DOT com
From: Dlanor Blytkerchan <dlanor AT dds DOT nl>
Subject: Re: String arrays...
Reply-To: djgpp AT delorie DOT com
X-Mailing-List: djgpp AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

>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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019