reading from a text file

S

slickn_sly

I'm trying to read a text file and storing each line to a specific
array.
so if i define an array - name, address,... and in my text file I have
something like:

John Doe
Seattle, Washington

Julie Walsh
New York, New York

I wanna store the names into the name array and the addresses into the
address array. Anybody have any ideas?
Posted at: http://www.groupsrv.com
 
W

Walter Roberson

I'm trying to read a text file and storing each line to a specific
array.
so if i define an array - name, address,... and in my text file I have
something like:
John Doe
Seattle, Washington
Julie Walsh
New York, New York
I wanna store the names into the name array and the addresses into the
address array. Anybody have any ideas?

Hmmm, that sounds like a question from an introductory programming
class.

Is there a given maximum size for the number of name/address pairs?
Is there a given maximum size for each name? For each address?

Generally speaking, you start by allocating the name and address
arrays. There will be several entries for each, and each -entry-
is a string. But a string is an array of characters, so you are in
effect going to need a pair of two-dimensional arrays. You can put
that into effect in more than one way, one of which is conceptually
simple and the other of which is much more efficient.
 
C

CBFalconer

slickn_sly said:
I'm trying to read a text file and storing each line to a specific
array. so if i define an array - name, address,... and in my text
file I have something like:

John Doe
Seattle, Washington

Julie Walsh
New York, New York

I wanna store the names into the name array and the addresses
into the address array. Anybody have any ideas?

First, get and compile the source to ggets, which is available at:

<http://cbfalconer.home.att.net/download/ggets.zip>

and contains only portable standard C. Then you can write your
code and link in ggets.o. The code will probably be something like
(untested):

#include <stdio.h>
#include <stdlib.h>
#include "ggets.h"

#define MAXGUYS 25 /* or whatever floats your boat */

int main(void)
{
int ix, iy;
char *name[MAXGUYS]
char *address[MAXGUYS];
char *blankline;

ix = 0;
while ((0 == ggets(&name[ix]) && (0 == ggets(&address[ix]))) {
if (++ix >= MAXGUYS) break;
if (0 == ggets(&blankline)) free(blankline);
else {
puts("Badly formatted input file");
break;
}
}
for (iy = 0; iy < ix; iy++) {
printf("%s\n%s\n\n", name[iy], address[iy]);
free(name[iy]); free(address[iy]);
}
return EXIT_SUCCESS;
} /* main, untested */

(Vs guvf vf ubzrjbex, juvpu vg unf gur fzryy bs, rvgure uvf
vafgehpgbe jba'g oryvrir ur ohvyg guvf naq jvyy tvir uvz na S, be
gur vafgehpgbe vf fb thyyvoyr gung vg ernyyl qbrfa'g znggre. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top