Lesson 1: Transferring Data with WRITEU
In this lesson, you will create a PV-WAVE program that creates a binary data file containing a 4-by-4 array of floating-point values, and then create a C program that reads and displays that binary data file.
To use PV-WAVE to create a binary data file, do the following:
1.Open a file for output.
OPENW, 1, 'float.dat'
2. Write a 4-by-4 array with each element set equal to its one-dimensional index.
WRITEU, 1, FINDGEN(4, 4)
3. Close the file.
CLOSE, 1
You are now ready to write the C program that reads this binary file.
To create the C program to read and display float.dat
, do the following:
1.Write or copy the following C program.
#include <stdio.h>
main()
{
float data[4][4];
FILE *infile;
int i, j;
infile = fopen("float.dat", "r");
(void) fread(data, sizeof(data), 1, infile);
(void) fclose(infile);
for (i=0; i < 4; i++)
{
for (j=0; j < 4; j++)
printf("%8.1f", data[i][j]);
printf("\n");
}
}
2. Run this program. Your output should look similar to the following:
0.0 1.0 2.0 3.0
4.0 5.0 6.0 7.0
8.0 9.0 10.0 11.0
12.0 13.0 14.0 15.0