Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>%s</code> conversion specification stops reading at the first white space, and tabs and blanks both count as white space.</p> <p>If you want to read a string of non-tabs, you can use a 'scan set' conversion specifier:</p> <pre><code>if (fscanf(fp, "%d\t%[^\t\n]\t%[^\t\n]", &amp;t-&gt;num, t-&gt;string1, t-&gt;string2) != 3) ...oops - format error in input data... </code></pre> <p>(I'd lay odds that omitting the <code>&amp;</code> from the string arguments is correct.) <em>The question was edited; I win. Dropping the <code>&amp;</code> is necessary to avoid the compiler warning!</em></p> <p>This still doesn't quite do what you expect. If there are blanks at the start of the second field, they'll be eaten by the <code>\t</code> in the format string. Any white space in the format string eats any white space (including newlines) in the input. The <code>%[^\t]</code> conversion specification won't get started until there's a character that isn't white space in the input. I'm also assuming you want your input limited by newlines. You can leave out the <code>\n</code> characters if you prefer.</p> <p>Note that I checked that the <code>fscanf()</code> interpreted 3 fields. It is important to error check your inputs.</p> <p>If you really want control, you should probably read whole lines with <code>fgets()</code> and then use <code>sscanf()</code> to parse the data.</p> <hr> <blockquote> <p>About <code>fgets()</code> and <code>sscanf()</code>; can you expand about how it will give more control?</p> </blockquote> <p>Suppose the input data is written</p> <pre><code>1234 a string with spaces another string </code></pre> <p>spread out over multiple lines like that. With raw <code>fscanf()</code>, this will be acceptable input even though it is spread over 9 lines of input. With <code>fgets()</code>, you can read a single line, and then analyze it with <code>sscanf()</code>, and you'll know that the first line was not in the correct format. You can then decide what to do about it.</p> <p>Also, since <a href="https://stackoverflow.com/users/1741125/mafso">mafso</a> called me on it in his <a href="https://stackoverflow.com/questions/20803245/how-to-write-and-read-including-spaces-from-text-file/20803374?noredirect=1#comment31189462_20803374">comment</a>, we should ensure that there are no buffer overflows by limiting the size of the strings that the scan sets match.</p> <pre><code>if (fscanf(fp, "%d\t%14[^\t\n]\t%14[^\t\n]", &amp;t-&gt;num, t-&gt;string1, t-&gt;string2) != 3) ...oops - format error in input data... </code></pre> <p>I'm using the error message about <code>char (*)[15]</code> to deduce that 14 is the correct number to use. Note that unlike <code>printf()</code>, you can't specify the sizes via <code>*</code> notation (in the <code>scanf()</code>-family, <code>*</code> supresses assignment), so you have to create the format with the correct sizes. Further, the size you specify is the number of characters before the terminating null byte, so if the array is of size 15, the size you specify in the format string is 14, as shown.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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