Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following worked quite well for my use case. I wanted to read the first two fields of a TAB-delimited file into string vars, then read the remainder of each line into a final string var.</p> <p>Here's the code:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; int main() { unsigned char string1 [255]; unsigned char string2 [255]; unsigned char string3 [255]; /* read from stdin until done */ while(!feof(stdin)) { fscanf( stdin, "%[^\t]\t%[^\t]\t%[^\n]\n", string1, string2, string3 ); printf( "%s\t%s\t%s\n", string1, string2, string3 ); } return(0); } </code></pre> <p>I'm reading from STDIN because I used this program to create a command line filter.</p> <p>Explanation of the fscanf codes:</p> <pre><code>%[^\t] - any character that is not a TAB \t - the TAB character %[^\n] - any character that is not a NEWLINE \n - the NEWLINE character </code></pre> <p>Thus, my fscanf is reading all characters up to the first TAB (including spaces but not the TAB itself) and placing the string into var string1, all characters up to the second TAB (including spaces but not the TAB itself) and placing the string into var string2, then reading all remaining characters of the record (TABs, spaces, everything except the NEWLINE) up to the NEWLINE into string3.</p> <p>In my real program, I am doing specific processing on string1 and string2. My output is the result of that processing along with string3. In other words, my output is also TAB-delimited with the original contents of string3 unaltered.</p> <p>If you have a TAB-delimited file with three or more fields, then the following (on Linux) should be true:</p> <pre><code>cat FILE | ABOVE_PROGRAM &gt; OUT_FILE diff FILE OUT_FILE # This should yield nothing (no differences) </code></pre> <p>Hopefully this will help others process TAB-delimited files.</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.
    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