Into an array of structure arrays.

S

Steve Chow

rough example I have two structs

typedef struct
{
int yaxis;
int xaxis;
int length;
}Cords;

typedef struct
{
int number;
char type;

Cords *cords;
}Province;



I have a file that looks like

1 //Should be read into Province.number
20 40 80 //Should be read into Province.Cords.yaxis xaxis and length
respectively
21 39 80
.......
9 // 9 Terminates


2
50 80 20
.......
9


So basically, I need a way of pouring an unknown amount of blocks that
terminate with 9 from a file, into an unknown amount of Province
structures, when it gets to the sole #9 it should move onto another
Province structure.. ideas? (or better ideas to indicate that it should
move onto another structure?)

examples please.
 
A

Artie Gold

Steve said:
rough example I have two structs

typedef struct
{
int yaxis;
int xaxis;
int length;
}Cords;

Why would you use a typedef like this in C++. Unlike C, where `struct's
have their own namespace, in C++ using the simple:

struct Cords {
int ...
...
};

creates a type called `Cords'.
typedef struct
{
int number;
char type;

Cords *cords;

Why are you not using a std::vector here? It's just the Right Way (tm)
to go.
}Province;



I have a file that looks like

1 //Should be read into Province.number
20 40 80 //Should be read into Province.Cords.yaxis xaxis and length
respectively
21 39 80
......
9 // 9 Terminates


2
50 80 20
......
9


So basically, I need a way of pouring an unknown amount of blocks that
terminate with 9 from a file, into an unknown amount of Province
structures, when it gets to the sole #9 it should move onto another
Province structure.. ideas? (or better ideas to indicate that it should
move onto another structure?)

examples please.

Well, if you insist upon trying to do it the `C' way, you'll have to
allocate space on the fly and use realloc() when you need more. A common
strategy is to double the number of potential entries whenever you run
out and do a final trim when you're done.

Using std::vector, however, is clearly the way to go.

==========

One more quick note: You should always show what you've tried; just
asking: "How do I do it?" is not nearly as likely to get you help, nor
is the help you get likely to be as valuable in the long run.


HTH,
--ag
 
B

Ben Pope

Steve said:
rough example I have two structs

typedef struct
{
int yaxis;
int xaxis;
int length;
}Cords;

typedef struct
{
int number;
char type;

Cords *cords;
}Province;

Prefer to create the types like this:

struct Cords {
int yaxis;
int xaxis;
int length
};

struct Province {
int number;
char type;
Cords *cords;
};

Thats how you do it in C++, rather than C.
I have a file that looks like

1 //Should be read into Province.number
20 40 80 //Should be read into Province.Cords.yaxis xaxis and length
respectively
21 39 80
......
9 // 9 Terminates


2
50 80 20
......
9


So basically, I need a way of pouring an unknown amount of blocks that
terminate with 9 from a file, into an unknown amount of Province
structures, when it gets to the sole #9 it should move onto another
Province structure.. ideas? (or better ideas to indicate that it should
move onto another structure?)

It would have been easier if you'd actually given us a sample of just
the file, without the annotations.

You can create a stream operators:

#include <istream>

istream& operator>>(istream &is, Provice &obj);
istream& operator>>(istream &is, Coords &obj);

Start with Coords, come back here with your code when you have that
working :)

Ben Pope
 
S

Steve Chow

"Why are you not using a std::vector here? It's just the Right Way (tm)
to go. "

I was aware that I could create a vector of structures, didn't think it
was possible to have a vector of structures as a member of the
structure. The last time I tried doing that I got some weird errors,
I'll try again when I have access to a compiler.

As for the rest of the stuff, I'm a C programmer who usually just
resorts to using a few C++ things out of laziness, my knowledge of it
as such is rather limited.

I think I have an idea though, and will post my cade later.
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top