NAME
fread, fwrite - binary stream input/outputSYNOPSIS
#include <stdio.h> size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
Note:
3rd Argument is sizeof nmemb: nmemb -> number of size to be written into fiile. Eg: if 1 then only one node will be written into file. if 2 then two nodes will be written into the file.
DESCRIPTION
- The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.
- The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.
RETURN VALUE
- On success, fread() and fwrite() return the number of items read or written. This number equals the number of bytes transferred only when size is 1.
- If an error occurs, or the end of the file is reached, the return value is a short item count (or zero).
- fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred.
- Note: To verify error need to use ferror() API, could not use return value.
Program:
/* Write binary(struct) data to the file using fread, fwrite
* Check : http://velrajcoding.blogspot.in
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define FILE_NAME "binary_fil"
#define FILE_NAME_2 "binary_fil2"
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
typedef struct node {
int value1;
int value2;
char name[64];
} node_t;
void display_node(node_t *node, char *msg)
{
if (!node || !msg) {
printf("Node or msg is null \n");
return;
}
printf("Node %s Value 1:%d Value2:%d name:%s \n", msg, node->value1, node->value2, node->name);
return;
}
int file_write_struct(node_t *node, const char *file, size_t num_node)
{
FILE *fp = NULL;
if (!(fp = fopen(file, "w"))) {
printf("Error in open an file, error no:%d:<%s> \n", errno, strerror(errno));
return FALSE;
}
/*
* 3rd Argument is sizeof nmemb:
* nmemb -> number of size to be written into fiile.
* Eg: if 1 then only one node will be written into file.
* if 2 then two nodes will be written into the file.
*/
fwrite(node, sizeof(node_t), num_node, fp);
if (ferror(fp)) {
printf("Error in write to the file, error no:%d:<%s> \n", errno, strerror(errno));
return FALSE;
}
fclose(fp);
return TRUE;
}
int file_read_struct(node_t *node)
{
FILE *fp = NULL;
if (!(fp = fopen(FILE_NAME, "r"))) {
printf("Error in open an file, error no:%d:<%s> \n", errno, strerror(errno));
return FALSE;
}
fread(node, sizeof(node_t), 1, fp);
if (ferror(fp)) {
printf("Error in write to the file, error no:%d:<%s> \n", errno, strerror(errno));
return FALSE;
}
fclose(fp);
return TRUE;
}
int main()
{
node_t node[] = { {100, 200, "Velraj Kutralam"},
{20, 35, "Kutralam S"} };
node_t node_read = {0};
display_node(node, "before save");
file_write_struct(node, FILE_NAME, 1);
file_write_struct(node, FILE_NAME_2, 2);
file_read_struct(&node_read);
display_node(node, "after save");
return 0;
}
Output:
:~/velrajk/sample$ ./a.outNode before save Value 1:100 Value2:200 name:Velraj Kutralam
Node after save Value 1:100 Value2:200 name:Velraj Kutralam
:~/velrajk/sample$ cat binary_fil
d▒Velraj Kutralam:~/velrajk/sample$
:~/velrajk/sample$ cat binary_fil2
d▒Velraj Kutralam#Kutralam S:~/velrajk/sample$
No comments:
Post a Comment