hello i have the following

char Ambiglist[1000];

this variable contains a company name and postcode, but there may be numerous of both ie

STEPHEN COMPANY LTD;TN25 8a7 JOHN JACKSON LTD;98asHHS HUMBURG LTD;8JSHAKS

i want to read in and store the company names in a variable, but i cant for the life of me get it to work

Any assistance?

regards

Recommended Answers

All 8 Replies

You're going to need to write a parser to seperate the information.

First, loop through each charecter. Make an array of strings to store the parsed data. If the charecter a semicolon, start storing data in the next variable. If it's a regular charecter, store the data. (Use a unsigned integer to keep track of which element you're storing the data in the array.) And if it's a \0, it means you've reached the end of the string.

Hope this helps

In order to parse the string properly, you need to have some way to identify where each record begins and ends. You could do this by adding some kind of delimiter character (One which won't be used in any other part of the string), eg, using a delimiter of '$'

char Ambiglist[] = "STEPHEN COMPANY LTD;TN25 8a7$JOHN JACKSON LTD;98asHHS$HUMBURG LTD;8JSHAKS$";

Then you can search through the string to find the position of the end of the first record/beginning of the second record, and extract this to a new string - repeat until you've seperated each record from the string.

Unfortunatly there is no way of knowing where to put the delimiter

Unfortunatly there is no way of knowing where to put the delimiter

I don't quite know what you mean...

Anyway, you *can* parse the data without a record delimiter charecter, assuming that each record contains exactly 2 fields... in that case, you just have to alternate array variables when storing the parsed data. Although if you can, do what Bench said, because it'll make the parsing easier in the long run.

i want to read in and store the company names in a variable, but i cant for the life of me get it to work

Haven't you been here long enough to know that without knowing what you've tried (code) we can't help? We ain't psychic!!

Member Avatar for gcs584

You're going to need to write a parser to seperate the information.

...or you could save yourself a lot of time and utilize the following function by including string.h:

char * strtok ( char * str, const char * delimiters )

where:
str is your original string to search through
delimeters is a pointer to an array of delimeters

...or you could save yourself a lot of time and utilize the following function by including string.h:

char * strtok ( char * str, const char * delimiters )

where:
str is your original string to search through
delimeters is a pointer to an array of delimeters

And what are the delimiters? The example string shows a name and a postal code separated by semicolons, but there are multiple pairs on a line and the only thing separating them is whitespace. I see what looks like a postal code with internal whitespace too, but you can't use whitespace as a delimiter or you would get this.

STEPHEN
COMPANY
LTD;TN25
8a7
JOHN
JACKSON
LTD;98asHHS
HUMBURG
LTD;8JSHAKS

You can't use a semicolon as a delimiter because then you would merge different records.

STEPHEN COMPANY LTD
TN25 8a7 JOHN JACKSON LTD
98asHHS HUMBURG LTD
8JSHAKS

The only way to get strtok to work is to introduce delimiters at the right places, and sgriffiths said he doesn't know where to do that. Worser still, the program could be reused with a file of millions of records and you can't expect people to manually add delimiters to that.

With the given information, the problem isn't solvable. We need a better definition of the difference between the two parts of a record or a better format for the string.

Member Avatar for gcs584

And what are the delimiters? The example string shows a name and a postal code separated by semicolons, but there are multiple pairs on a line and the only thing separating them is whitespace. I see what looks like a postal code with internal whitespace too, but you can't use whitespace as a delimiter or you would get this.

I'm well aware of what you state. My post was meant to complement Bench's post. Obviously, with the current vague problem specification provided, the data could be interpreted in many different ways.

Nonetheless, the function could still be deemed useful once the problem specification is clarified or better understood.

Kind Regards,

GCS584

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.