Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The process is to use the <code>strtok</code> function. What the function does is change the delimiter (in this case a <code>,</code>) into a <code>\0</code> (null) character. It then passes back a pointer to the start of the string now terminated by the <code>\0</code>. If run again with a NULL passed into it, it will then split based on the delimiter again (if it exists), and pass back a pointer to the second portion of the string. The following is the code you need to add:</p> <pre><code>#include &lt;string.h&gt; char* var1; char* var2; </code></pre> <p>And then in your while loop:</p> <pre><code>{ var1 = strtok(text,","); var2 = strtok(NULL,","); printf("%s&lt;NOT A ','&gt;%s\n", var1,var2); } </code></pre> <p>What this is doing to the string text is as follows. When we get it from the file, <code>text</code> =</p> <pre><code>/------------------------------------\ |u|s|e|r|n|a|m|e|,|p|a|s|s|w|o|r|d|\0| \------------------------------------/ </code></pre> <p>The first line of the while loop changes it so that <code>text</code> = </p> <pre><code>/-------------------------------------\ |u|s|e|r|n|a|m|e|\0|p|a|s|s|w|o|r|d|\0| \1------------------------------------/ </code></pre> <p>Where <code>var1</code> points to the letter above the 1, thus <code>var1 = username</code>. Note that this means if you run a printf on <code>text</code>, you will also get username. After the second line, <code>text</code> = </p> <pre><code>/-------------------------------------\ |u|s|e|r|n|a|m|e|\0|p|a|s|s|w|o|r|d|\0| \1------------------2-----------------/ </code></pre> <p>And now <code>var1</code> points to a string "username" and <code>var2</code> points to the string "password". If you printf <code>text</code>, you will still get "username".</p> <p>Note that if you were wanting to get more then two variables, you would want to have a safety catch making sure that strtok didn't return a <code>NULL</code> before you try and use the string. </p> <p>Also note that if you modify <code>text</code>, you will also mess up <code>var1</code> and <code>var2</code>. I would suggest using a <code>strcpy</code> to another string if you wish to hold the information in a more permanent way.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload