I don't think that you have to use a getline function. For instance, I will use the
fscanf(file_pointer, "%s", your_string)
function because you will read the lines from a file.
Here I go:
If you know exactely how many lines is in that file, is very easy. You use a for-loop
for(contor=0; contor<lines_number; contor++)
. Now, you use the fscanf function, described below (if you want more information about it, is in the reference of that site) for reading the line. You have to work on a single line (you read the line then you work with it and then the vector (the string) work with another line) so in that loop, you have to make all the operations you need on that vector.
After you read the line, you have to work with it. You can do it like this:
1 2 3 4 5
|
for(contor_4=0; isspace(your_string[contor_4])!=0; contor_4++)
{ vector_2=your_string[another_contor]; }
for(contor_3=contor_4; your_string[contor_3]; contor_3++)
{ vector_3=your_string[contor_3]; }
}
|
This is just a suggestion. I know that others people on this forum have a better ideas.
In vector_2 you get a part of that line (for instance the name or that dates)
In vector_3 you get the other part of that line (for instance that dates or the name)
For doing this, the lines in the file should be like this: 1262011 John. This is a simple way because the line is splitted in 2 parts: the numbers and the name. Between them is a space. And you can easely read those to parts and memorize them in 2 separated vectors. You can also make the line like this: 12 6 2011 character Jonh. Where I wrote "character", you can put any character you want and to read that two parts of the line (the numbers and the name) you read the string (and you put it in a vector) untill you reach that character. When you have reached the character, you keep the current value of the contor and then you read the last part of the line (you must do that with a for-loop and the condition of the loop must be
your_string;
which is equivalent with
your_string!=NULL
and you put it in another vector. This is the generalized version of the algorithm because in the first version, between those parts it was space which is a character. If you use this version, don't worry about the printing part of the numbers: they are stored in the vector like this: 1262011 but when you print them, you ca easely insert a space wherever you want like this:
cout<<vector_numbers[0]<<vector_numbers[1]<<" "<<vector_numbers[2]<<...
I hope you understood the idea.
So, now, we stored the numbers and the numbers in two separated vectors. A very important aspect is the index of the vectors. The first component of the numbers_vector (the vector where are stored the numbers) has the correspondant the first component in the names_vector ( the vector where are stored the names). This is very important.
Now, you've stored 30 names and dates (this is an example). There is possible that 2 or 3 or 10 names to be the same. The idea is like that: if you want to print all the dates of a name, you have to find the indexes of that number and then to print the elements in the numbers_vector which have that indexes. More clearly: a name is stored 3 times in names_vector like that: names_vector[2], names_vector[4] and names_vector[10]. Those components has stored in them the "Jack" name. The indexes of those components which have stored in them the same name are: 2, 4 and 10. You use that indexes to print the numbers_vector[2], numbers_vector[4] and the numbers_vector[10]. I hope I was clear enough. I can't write some code for you right now because I'm tired but if you really want that, and if you didn't understand all those things that I am trying to explain here, I will try something for you.