/* Sequential Files #3 */ #include #include #include #include #include #include void Pause() { printf ("\n Press Anykey to continue..."); getch (); } int main () { clrscr (); FILE *fp; const SIZE = 10; int i, value, array[SIZE]; const row = 4; const col = 6; int matrix[row][col]; for (i = 0; i < SIZE; i++) { array[i] = 2*i; } fp = fopen ("dumb.txt", "w+b"); if (!fp) { fprintf (stderr, "Error opening file."); exit (1); } //SIZE = row * col; //fwrite (matrix, sizeof (int), SIZE, fp); for (i = 0; i < row; i++) { int j; for (j = 0; j < col; j++) { fwrite (&matrix[i][j], sizeof (int), SIZE, fp); } } fclose (fp); int size; fp = fopen ("numb.txt", "rb"); if (!fp) { fprintf (stderr, "Error opening file."); exit (1); } fread (&SIZE, sizeof (int), 1, fp); int *array2; array2 = (int *) malloc (size * sizeof (int)); fread (array2, sizeof (int), size, fp); fclose (fp); puts ("Array values:"); for (i = 0; i < size; i++) printf ("%5d", array2[i]); free (array2); //fwrite (array, sizeof (int), SIZE, fp); //fwrite (sizeof (int), array, SIZE, fp); rewind (fp); for (i = 0; i < SIZE + 1; i++) { fread (&value, sizeof (int), 1, fp); //fread (sizeof (int), value, 1, fp); printf ("%5d", value); } fclose (fp); printf ("\n"); fp = fopen ("dumb.txt", "r+b"); if (!fp) { fprintf (stderr, "Error opening file."); exit (1); } fwrite (&SIZE, sizeof (int), 1, fp); fwrite (array, sizeof (int), SIZE, fp); rewind (fp); for (i = 0; i < SIZE + 1; i++) { fread (&value, sizeof (int), 1, fp); printf ("%5d", value); } fclose (fp); /* char name [80]; puts ("What is your name?"); gets (name); puts ("\nHello,"); puts (name); */ Pause (); return 0; }