C++/c++ strings
Expert: Joseph Moore - 9/5/2009
Questionhi
I'm trying to do a programm that reads a file, which has this format : each line as a number and a full name ( like: 2 John mcvil johnson ) and prints on the screen the number and the person's first and last name.
I already thought about it and I wrote something like
while (file_name.get() !=EOF)
{
strig.find(' ');
}
but this will stop in the first space (the one between the first and the second name)
how do I do for it to go on until the last space ??
I also saw somewhere that the while sentence should be while (getline(file_name,string, '/n'))
can you explain me why in a easy way ? I didn't really understand 'cause I don't have much experience in programming.
thanks.
AnswerHi, Gong.
You are on the right track here. Your idea is solid, and exactly what needs to be done, There's just a small issue with how you're going about it.
You did see correctly that the while should be getting a line at a time. What you want to do is grab a full line, then parse that line on its own. There is a really handy function to help you parse the line called strtok:
char* strtok(char* str, const char* delimiters);
The strtok function, as you can see, takes two parameters. The first parameter is the string you are going to tokenize (or split into smaller pieces, called tokens). The second parameter is another string that defines the delimiters, or characters that split the string into tokens. So, if you had a string:
"this is my super awesome string. it's the best!"
And you wanted each word, without punctuation, your delimiter set would be:
" .!"
Please note that in the above examples, the quotation marks are there as they would be in code. They are not actually part of the string, but rather contain the string. There is a bit of a trick to how the strtok function works, though. Look at the following code:
char str[] ="- This, a sample string.";
char * pch;
pch = strtok (str," ,.-");
printf(%s\n", pch);
pch = strtok (str," ,.-");
printf(%s\n", pch);
pch = strtok (str," ,.-");
printf(%s\n", pch);
The output of the above code would be:
This
This
This
Notice that it's only grabbing the first word every time. That's because of the way strtok works. You *initialize* it with a string. Once it's initialized with a string, you do not continue to provide the string in successive strtok calls, and it will continue to process the original string. Proper use of strtok looks something more like:
char str[] ="- This, a sample string.";
char * pch;
pch = strtok (str," ,.-");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.-");
}
Notice how the successive calls to strtok use NULL as the input string. I also recommend creating a string to contain the delimiters, so that you don't have to modify the string in two places if you decide to add or remove delimiters.
I hope this answers all your questions. If you have remaining questions, if I have raised new questions, or if you just think of some different questions, feel free to let me know. I'm here to help. :)