C: FileI/O looping through a file containing a set of numbers to find their squares, cubes, and square roots
<p>The objective of the program is to read a set of numbers from a text file loop through each number in the file find the Square, Cube, and the Square root of each number and write the new numbers in an output text file.</p>

<p>I've figured out how to read and write to files properly but i cant figure out how to format the loop properly to read each of the number from the input file.</p>

<p>i also attempted to use the "sqrt()" function in math.h, but it did not work correctly i am more concerned with getting the loop working properly first.</p>

<pre><code>   #include &lt;stdio.h&gt;
   #include &lt;ctype.h&gt;
   #include &lt;math.h&gt;
   #define SIZE 40

  int main(void)
{
char ch, filename[SIZE]; //variables


int i, n, square, cube , sroot;

FILE *fp;
printf("Please enter the filename to read: ");
gets(filename);


if ((fp = fopen(filename, "r")) == NULL)
{
    printf("Cannot open the file, %s\n", filename);
}
else
{
    while (fscanf(fp,"%d",&amp;n) == 1)
    {
      for(i=0; i&lt;=n; i++)
      {

      square=n*n;
      cube=square*n;
      sroot= sqrt(n);
      }
}
}


fclose(fp);

char filename2 [SIZE];
FILE *fp2;

fprintf(stdout, "Please enter the file name to write in: "); //asks for file that you want to write to
gets(filename2);

if ((fp2 = fopen(filename2, "w")) == NULL) //"w" for writing
{
    printf("Cannot create the file, %s\n", filename2);
}
else
{
    for(i=0; i&lt;=n; i++)
    {
        fprintf(fp2, "%d, %d, %d \n",square, cube, sroot);

    }

}

fclose(fp2); // closing the file
fprintf(stdout, "You are writing to the file, %s is done.\n", filename2);

return 0;
</code></pre>

<p>}</p>