Hi,
just started to learn working with files. Trying to run this code, which writes array to a file & then copy it another array and display it on screen:
Code:
#include<stdio.h>
#include<stdlib.h>
#define SIZE 20
int main()
{
int count , array1[SIZE], array2[SIZE];
FILE *fp;
for (count=0; count<SIZE;count++)
array1[count]=2*count;
if ((fp=fopen("direct.txt", "wb"))==NULL)
{
fprintf(stderr, "eror opening file (1)");
exit(1);
}
if (fwrite (array1, sizeof (int), SIZE, fp)!=SIZE)
{
fprintf(stderr, "error writing to file");
exit(1);
}
fclose (fp);
if ((fp=fopen("direct.txt", "rb"))==NULL)
{
fprintf (stderr, "error opening file (2)");
exit(1);
}
if (fread (array2, sizeof(int), SIZE, fp)!=SIZE);
{
fprintf(stderr, "error reading file! \n");
exit(1);
}
fclose (fp);
for (count=0; count<SIZE;count++)
printf ("%d\t%d\n", array1[count], array2[count]);
return (0);
}
...and the error:
error reading file!
which means all program runs OK, till the point where it should read data from binary file. Where was I wrong?