The following code writes into the file but is unable to retrieve fields of the struct
conf_record in the printf (after fread within while loop) statement.Pleas e give a solution.
[code=c]/* record.h */
typedef struct conf_record {
char *source_IP;
char *dest_IP;
char *s_port;
char *d_port;
//char** range_allow;//[2];
char *range_disallow[2];
} Record;
/* main.c */
#include <stdio.h>
#include <errno.h>
#include "record.h"
int main()
{
FILE *f1, *f2;
Record record, record1;
int i;
void *buff;
f1 = fopen("./conf.txt", "a");
if (f1 != NULL) {
do {
printf("\nenter source_IP and dest_IP \n");
scanf("%s%s", &(record.source _IP), &(record.dest_I P));
printf("\nenter source_port and dest_port\n");
scanf("%s%s", &(record.s_port ), &(record.d_port ));
printf("\nenter start and end time of restricted period\n");
scanf("%s%s", &(record.range_ disallow[0]),
&(record.range_ disallow[1]));
buff = &record;
fwrite(buff, sizeof(Record), 1, f1);
fflush(f1);
printf("\n more records? say 1 or 0 for yes /no:");
scanf("%d", &i);
} while (i == 1);
fclose(f1);
}
f2 = fopen("conf.txt ", "r");
if (f2 != NULL) { //reading not taking place!!!!!!
while (fread(buff, sizeof(Record), 1, f2) > 0) {
record1 = *((Record *) buff);
printf("%s %s %s %s", record1.source_ IP, record1.dest_IP ,
record1.s_port, record1.d_port) ;
}
fclose(f2);
}
return 0;
}[/code]
conf_record in the printf (after fread within while loop) statement.Pleas e give a solution.
[code=c]/* record.h */
typedef struct conf_record {
char *source_IP;
char *dest_IP;
char *s_port;
char *d_port;
//char** range_allow;//[2];
char *range_disallow[2];
} Record;
/* main.c */
#include <stdio.h>
#include <errno.h>
#include "record.h"
int main()
{
FILE *f1, *f2;
Record record, record1;
int i;
void *buff;
f1 = fopen("./conf.txt", "a");
if (f1 != NULL) {
do {
printf("\nenter source_IP and dest_IP \n");
scanf("%s%s", &(record.source _IP), &(record.dest_I P));
printf("\nenter source_port and dest_port\n");
scanf("%s%s", &(record.s_port ), &(record.d_port ));
printf("\nenter start and end time of restricted period\n");
scanf("%s%s", &(record.range_ disallow[0]),
&(record.range_ disallow[1]));
buff = &record;
fwrite(buff, sizeof(Record), 1, f1);
fflush(f1);
printf("\n more records? say 1 or 0 for yes /no:");
scanf("%d", &i);
} while (i == 1);
fclose(f1);
}
f2 = fopen("conf.txt ", "r");
if (f2 != NULL) { //reading not taking place!!!!!!
while (fread(buff, sizeof(Record), 1, f2) > 0) {
record1 = *((Record *) buff);
printf("%s %s %s %s", record1.source_ IP, record1.dest_IP ,
record1.s_port, record1.d_port) ;
}
fclose(f2);
}
return 0;
}[/code]
Comment