Note that there are some explanatory texts on larger screens.

plurals
  1. POvalidating content of input file
    primarykey
    data
    text
    <p>I have this defensive-programming issue that I don't really know how to solve. </p> <p>I have this function that takes file path and size of table (rows / columns count) as arguments and I'm looking for better way of validating the input file. I assume that arguments of this function are always correct. <code>size</code> represents the "smaller side" of the table that is stored in file:</p> <p>For example :</p> <pre><code>1 2 3 4 5 6 7 8 </code></pre> <p>size = 2 is correct while</p> <pre><code>1 2 3 4 5 5 6 7 8 9 </code></pre> <p>size = 2 is incorrect</p> <p>I'd also like to be able to reject the files like this </p> <pre><code>1 2 3 4 5 6 7 8 </code></pre> <p>size = 2 (which is accepted through fscanf)</p> <p>Another type of file I'd like to be able to reject is</p> <pre><code>1 2 3 4 5 6 </code></pre> <p>size = 2</p> <p>As for now my only security is checking if the elements of the file are really numbers. </p> <p>Here is the code I've done so far :</p> <pre><code>void import(float** table, int size, char* path) { FILE* data = fopen(path, "r"); assert(data); int i,j; int st; for (i=0; i&lt;size; i++) { for(j=0; j&lt;(size*2)-1; j++) { st = fscanf(data, "%f", &amp;table[i][j]); if (!st) { printf("Error while importing the file.\n"); fclose(data); return -1; } } } fclose(data); } </code></pre> <p>I don't really where and how to start, I'm not really proficient in C and there seems to exist a lot of functions and mechanisms to do what I want but they all seem very complex and some are actually longer than the code I provided. </p> <p>If anyone can point me in the right direction that'd be great.</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.
 

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