Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following code will read a file a line at a time</p> <pre><code>char line[80] FILE* fp = fopen("data.txt","r"); while(fgets(line,1,fp) != null) { // do something } fclose(fp); </code></pre> <p>You can then tokenise the input using <a href="http://msdn.microsoft.com/en-us/library/2c8d19sb(VS.71).aspx" rel="noreferrer">strtok()</a> and <a href="http://msdn.microsoft.com/en-us/library/zkx076cy(VS.71).aspx" rel="noreferrer">sscanf()</a> to convert the text to numbers.</p> <p>From the MSDN page for sscanf:</p> <blockquote> <p>Each of these functions [sscanf and swscanf] returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.</p> </blockquote> <p>The following code will convert the string to an array of integers. Obviously for a variable length array you'll need a list or some scanning the input twice to determine the length of the array before actually parsing it.</p> <pre><code>char tokenstring[] = "12 23 3 4 5"; char seps[] = " "; char* token; int var; int input[5]; int i = 0; token = strtok (tokenstring, seps); while (token != NULL) { sscanf (token, "%d", &amp;var); input[i++] = var; token = strtok (NULL, seps); } </code></pre> <p>Putting:</p> <pre><code>char seps[] = " ,\t\n"; </code></pre> <p>will allow the input to be more flexible.</p> <p><em>I had to do a search to remind myself of the syntax - I found it <a href="http://msdn.microsoft.com/en-us/library/c37dh6kf(VS.71).aspx" rel="noreferrer">here in the MSDN</a></em></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. 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