You are here:

C++/c++ strings

Advertisement


Question
hi
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.  

Answer
Hi, 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. :)

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joseph Moore

Expertise

I've been programming in one form or another since my brother taught me BASIC when I was 6. I've been programing professionally since I was 20, first web development with HTML, JS, DHTML, CSS, etc., then I became a video game developer, writing code in C, C++, C#, SQL, assembly, and various scripting languages. I've even written my own scripting languages, custom designed for the games I was making. I also dabble in Java, PHP, and Perl. I've worked on pretty much every aspect of game development, including graphics, audio, gameplay, tool, UI, input, animation, and physics.

Experience

I've been writing C++ code for 12 years, both on my own in my spare time and professionally.

Organizations
IGDA

Education/Credentials
Bachelor of Science in Game Design and Development, Full Sail University, Winter Park, FL

Awards and Honors
Salutatorian and Advanced Achiever Awards at Full Sail; Independent Games Festival Student Showcase winner, 2004; Featured article on Gamasutra about an experimental game developed in 2004

©2016 About.com. All rights reserved.