 <p>of course you can store it in a local variable and assign only if valid. But since you're calling <code>exit(0)</code> if invalid it doesn't change anything. I suppose you want to <code>break</code> from the loop instead.</p> <p>BTW your loop is wrong. you have to divide <code>sizeof(numArray)</code> by the size of one element otherwise you'll loop too many times and you'll crash the machine if there are too many numbers in your input file (yes, I also added a test for end-of-file)</p> <pre><code>if (myFile != NULL) { for (int i = 0; i &lt; sizeof(numArray)/sizeof(numArray[0]); i++) { int x; //insert int to array if (fscanf(myFile, "%d", &amp;x)==0) { printf("Invalid number found / end of file, closing application...\n"); exit(0); // end of file / not a number: stop } //Validate number if (x &gt; 25) { printf("Invalid number found, closing application...\n"); exit(0); } numArray[i] = x; } //close file fclose(myFile); } </code></pre>