473,397 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Into an array of structure arrays.

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.

Feb 15 '06 #1
3 1551
Steve Chow wrote:
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
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Feb 15 '06 #2
Steve Chow wrote:
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
--
I'm not just a number. To many, I'm known as a string...
Feb 15 '06 #3
"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.

Feb 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kaptain524 | last post by:
Hello, I am using PHP 5.0.4 with Apache 2, on WinXP Pro. This behavior appears to be fundamental however, and should not be affected by platform. It would seem that there is some kind of bug...
1
by: Doug C via .NET 247 | last post by:
Using C#... I am pulling shared memory in to my app that is in the form of apredefined structure. I have arrays in 2 sub-structures. Onearray is an array of another predefined structure, and the...
3
by: Michael Van Altena via .NET 247 | last post by:
I'm trying to figure out how to read a formatted binary file intoa structure definition in C#. I've tried using the"StructLayout" attribute with both LayoutKind.Explicit andLayoutKind.Sequential...
2
by: hokieghal99 | last post by:
I wish to place all files and directories that are within a user defined path (on a Linux x86 PC) into some type of array and then examine those items for the existence of certain charaters such as...
2
by: Neo | last post by:
Thanks to all those who responded. Your tips really worked. So I will give a simple program to explain, how I did it. #include<stdio.h> void update(); struct fields { char f1; char f2; char...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
6
by: n00dle | last post by:
hi, see if i want to copy the contents of a char arrary into a equally size struct, assuming that the structure has no holes, padding etc., i can use structure = *(t_structure *) array; but...
1
by: Jeff | last post by:
I am struggling with the following How do I marshal/access a pointer to an array of strings within a structure Than Jef ----------------------------------------------------------------
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
57
by: buuuuuum | last post by:
why array can't be assigned, like structs?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.