B
Blankdraw
I have a program below that runs fine. It accesses a datafile for
random-access transfers. Supposedly, it does this by accessing the
file in binary mode. I do not understand how it does this without
any such parameter. The lower-level open() functions use a |O_BINARY
or some such specification.
Q: Can someone tell me how I can use/modify this program and be left
with a datafile that has sensible (read: ASCII) data in it???
/* RWARRAY.C - Loads and saves an array from a database file on-disk
*/
#include <stdio.h>
#include <string.h>
#include <process.h>
#define ITEMS 7
int main()
{
char filename[85];
int count;
FILE *fileptr;
int data[ITEMS] = {8, 57, 5, 309, 33, 87, 55};
int data2[ITEMS];
printf("\nEnter filename: ");
gets(filename);
/* write array to file */
if ((fileptr = fopen(filename, "w")) == NULL)
{
printf("Error: Cannot open your pants\n");
exit(0);
}
printf("Writing data items to file %s....\n", filename);
fwrite(data, sizeof(data), 1, fileptr);
fclose(fileptr);
/* Read file into array */
if ((fileptr = fopen(filename, "r")) == NULL)
{
printf("Error: Cannot open your pants\n");
exit(0);
}
printf("Reading data items from file....\n");
fread(&data2, sizeof(data), 1, fileptr);
fclose(fileptr);
printf("The elements of the array are: \n");
for (count = 0; count < ITEMS; count++)
{
printf("Element %d is %d\n", count, data2[count]);
}
return 0;
}
random-access transfers. Supposedly, it does this by accessing the
file in binary mode. I do not understand how it does this without
any such parameter. The lower-level open() functions use a |O_BINARY
or some such specification.
Q: Can someone tell me how I can use/modify this program and be left
with a datafile that has sensible (read: ASCII) data in it???
/* RWARRAY.C - Loads and saves an array from a database file on-disk
*/
#include <stdio.h>
#include <string.h>
#include <process.h>
#define ITEMS 7
int main()
{
char filename[85];
int count;
FILE *fileptr;
int data[ITEMS] = {8, 57, 5, 309, 33, 87, 55};
int data2[ITEMS];
printf("\nEnter filename: ");
gets(filename);
/* write array to file */
if ((fileptr = fopen(filename, "w")) == NULL)
{
printf("Error: Cannot open your pants\n");
exit(0);
}
printf("Writing data items to file %s....\n", filename);
fwrite(data, sizeof(data), 1, fileptr);
fclose(fileptr);
/* Read file into array */
if ((fileptr = fopen(filename, "r")) == NULL)
{
printf("Error: Cannot open your pants\n");
exit(0);
}
printf("Reading data items from file....\n");
fread(&data2, sizeof(data), 1, fileptr);
fclose(fileptr);
printf("The elements of the array are: \n");
for (count = 0; count < ITEMS; count++)
{
printf("Element %d is %d\n", count, data2[count]);
}
return 0;
}