-3

I'm still quite new to programming -- about two months in -- so if this is a really basic question, then I apologize. Going along with that, my terminology might be completely off. If it is, I'd greatly appreciate any help you might be able to offer with telling me the proper terms. I searched around the forums here for a bit, but couldn't find anything that answered my question. If you're aware of a topic that does, then please just link it below.

Onto the question.

Let's say that I have an external text file with a bunch of information in it. The information is divided into items, each item delineated from the next by '::'. Each item is divided into four fields, each field delineated from the next by '\'.

What I want to do is take one item's information out of the text file and place it into an array called info. I want to then take info and pass it to another function. This function will create four new arrays and then portion out field 1 to array 1, field 2 to array 2, etc.

Basically, how do I take an array, take a portion of that array and give it to another variable, then copy another portion of that array and give it to another array.

Example:

The External Text File looks like the following:

26::Female::Kentucky::Trauma\\34::Male::Michigan::Elective\\85::Male::Unknown::Trauma\\18:Female::Washington::Emergent 

Using fstream, I then take "26::Female:Kentucky::Trauma" and put it into an array called 'info', which is then passed to a function called Sort(char info[]).

How do I get Sort(char info[]) to take an array with "26::Female::Kentucky::Trauma" and turn it into four arrays such as:

Age: 26
Sex: Female
Location: Kentucky
Reason for Admission: Trauma

EDIT Array 1 looks like: 26::Female::Kentucky::Trauma

I then create four char arrays called, Age, Sex, Location, Reason. How do I get 26 into the Age array, Female into the Sex array, Kentucky into the Location array, and Trauma, into the Reason array?


I know that I could do this at the stage where I'm reading in from an external file, but it seems easier to do it this way for my purposes.

Thank you for your time.

Treecorn
  • 91
  • 1
  • 6
  • question definition doesn't match its declaration. Are you really asking **how to parse a text file?** If you have two arrays (being dynamic, typically realized using `std::vector`), you can just use a range-constructor that takes then begin and end iterators of the subarray you want to copy. For example, `std::vector copied_array(&original_array[idx_begin], &original_array[idx_end])`. – The Paramagnetic Croissant Apr 11 '15 at 19:58
  • I don't know what you mean by 'question definition doesn't match its declaration'. But, no, I'm not asking how to parse a text file. I'm asking how I can take a char array that has the symbols '::' interspersed throughout and transfer the information in between the '::' to other arrays. I'll try to make it more clear. – Treecorn Apr 11 '15 at 20:10
  • 1
    @Acorn_Tree `But, no, I'm not asking how to parse a text file.` But you *are* asking how to parse a stream of text, whether you realize it or not. Whether that text is from a file, a string buffer, doesn't matter. It's basically the same thing. Possible duplicate: http://stackoverflow.com/questions/236129/split-a-string-in-c – PaulMcKenzie Apr 11 '15 at 20:17

1 Answers1

0

Look at the documentation for the string class. The functions find_first_of and substr will be useful. Split the string when it finds :: or //. For example, 26::Female::Kentucky::Trauma would be split into 26 and Female::Kentucky::Trauma. This sounds like it may be an assignment, so I will not give a complete solution, but this should be enough to get you going.

Lucas
  • 132
  • 6