By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,905 Members | 1,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,905 IT Pros & Developers. It's quick & easy.

how to read fields of a struct from file

P: 7
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.Please give a solution.

Expand|Select|Wrap|Line Numbers
  1. /* record.h */
  2. typedef struct conf_record {
  3.     char *source_IP;
  4.     char *dest_IP;
  5.     char *s_port;
  6.     char *d_port;
  7. //char** range_allow;//[2];
  8.     char *range_disallow[2];
  9. } Record;
  10.  
  11. /* main.c */
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include "record.h"
  15. int main()
  16. {
  17.     FILE *f1, *f2;
  18.     Record record, record1;
  19.     int i;
  20.     void *buff;
  21.  
  22.     f1 = fopen("./conf.txt", "a");
  23.     if (f1 != NULL) {
  24.         do {
  25.             printf("\nenter source_IP and dest_IP \n");
  26.             scanf("%s%s", &(record.source_IP), &(record.dest_IP));
  27.             printf("\nenter source_port and dest_port\n");
  28.             scanf("%s%s", &(record.s_port), &(record.d_port));
  29.             printf("\nenter start and end time of restricted period\n");
  30.             scanf("%s%s", &(record.range_disallow[0]),
  31.                   &(record.range_disallow[1]));
  32.  
  33.  
  34.             buff = &record;
  35.             fwrite(buff, sizeof(Record), 1, f1);
  36.             fflush(f1);
  37.             printf("\n more records? say 1 or 0 for yes /no:");
  38.             scanf("%d", &i);
  39.         } while (i == 1);
  40.         fclose(f1);
  41.     }
  42.  
  43.     f2 = fopen("conf.txt", "r");
  44.     if (f2 != NULL) {           //reading not taking place!!!!!!
  45.         while (fread(buff, sizeof(Record), 1, f2) > 0) {
  46.  
  47.             record1 = *((Record *) buff);
  48.             printf("%s %s %s %s", record1.source_IP, record1.dest_IP,
  49.                    record1.s_port, record1.d_port);
  50.         }
  51.         fclose(f2);
  52.     }
  53.     return 0;
  54. }
May 27 '07 #1
Share this Question
Share on Google+
7 Replies


Savage
Expert 100+
P: 1,764
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.Please give a solution.

/* 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_IP) );
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_po rt, record1.d_port);
}
fclose(f2);
}
return 0;
}
U can use scanf for that.

For more info check this article ;)


Savage
May 27 '07 #2

P: 7
Thank you for help. I could read both using fread and fscanf in different copies of
the same proogram by allocating memory(malloc) char * fields of the struct or else by scanf/printf on each field separately .
May 29 '07 #3

AdrianH
Expert 100+
P: 1,251
Thank you for help. I could read both using fread and fscanf in different copies of
the same proogram by allocating memory(malloc) char * fields of the struct or else by scanf/printf on each field separately .
Is that a question?


Adrian
May 29 '07 #4

P: 7
Is that a question?


Adrian
Though fread reads the written record, it only repeatedly shows the last record read , as many times as many records were written into the file.

I'm able to get all records using fscanf, but i also want to know how it could be done using fread and void pointers. So, Please tell.
May 29 '07 #5

AdrianH
Expert 100+
P: 1,251
Though fread reads the written record, it only repeatedly shows the last record read , as many times as many records were written into the file.

I'm able to get all records using fscanf, but i also want to know how it could be done using fread and void pointers. So, Please tell.
Well, you will need to read it to somewhere, so you will need an array which you may have to reallocate if you run out of space, an array of pointers which you must allocate each element for and reallocate if you run out of space or some other type of container (preferably one that will do the allocation for you ;)).

So, have you used malloc() and free() before? What about them do you not understand?


Adrian
May 29 '07 #6

P: 7
Well, you will need to read it to somewhere, so you will need an array which you may have to reallocate if you run out of space, an array of pointers which you must allocate each element for and reallocate if you run out of space or some other type of container (preferably one that will do the allocation for you ;)).

So, have you used malloc() and free() before? What about them do you not understand?


Adrian
Yes I do understand the functions performed by malloc and free.Though I
use malloc(and calloc) often , I haven't worked much with free(may be because I wrote small programs ). For the allocations done in this program, i'm sending that section too.


Expand|Select|Wrap|Line Numbers
  1. #include "record.h"
  2. int main()
  3.     {
  4.       FILE * f1,*f2;
  5.       Record record,record1;
  6.        int i;
  7.       void *buff;
  8.  
  9.         record.source_IP=(char*)calloc(16,sizeof(char));
  10.         record.dest_IP=(char*)calloc(16,sizeof(char));
  11.         record.s_port=(char*)calloc(4,sizeof(char));
  12.         record.d_port=(char*)calloc(4,sizeof(char));
  13.         record.range_disallow[0]=(char*)calloc(10,sizeof(char));
  14.         record.range_disallow[1]=(char*)calloc(10,sizeof(char));
  15.  
  16.         record1.source_IP=(char*)calloc(16,sizeof(char));
  17.         record1.dest_IP=(char*)calloc(16,sizeof(char));
  18.         record1.s_port=(char*)calloc(4,sizeof(char));
  19.         record1.d_port=(char*)calloc(4,sizeof(char));
  20.         record1.range_disallow[0]=(char*)calloc(10,sizeof(char));
  21.         record1.range_disallow[1]=(char*)calloc(10,sizeof(char));
  22.  
  23.              f1=fopen("./conftemp.txt","wb");
  24.                if(f1!=NULL)
  25.                {
  26.                         do{
  27.                         printf("\nenter source_IP and dest_IP  \n");
  28.                                 scanf("%s",(record.source_IP));
  29.                             scanf("%s",(record.dest_IP));
  30.                             printf("\nenter source_port and dest_port\n");    
  31.                                 scanf("%s",(record.s_port));
  32.                             scanf("%s",(record.d_port));
  33.                                 printf("\nenter start and end time of restricted period\n");
  34.                                 scanf("%s",(record.range_disallow[0]));
  35.                                 scanf("%s",(record.range_disallow[1]));
  36.  
  37.                                buff=&record;
  38.                                fwrite(buff,sizeof(Record),1,f1);
  39.                                fflush(f1);
  40.  
  41.                                printf("\n more records? say 1 or 0 for yes /no:");
  42.                               scanf("%d",&i);
  43.                           }  while(i==1);
  44.                        fclose(f1);  
  45.                }
  46.  
  47.               f2=fopen("./conftemp.txt","rb"); 
  48.               if(f2!=NULL)
  49.                    {
  50.                          while(fread(buff,sizeof(Record),1,f2)>0)    
  51.                                {
  52.                                  record1=*((Record*)buff);    //reads but repeats the last   
  53.                      //  record input to file instead of showing from the initial input???
  54.  
  55.                                 printf("\n");
  56.                                 printf("%s ",(record1.source_IP));
  57.                                 printf("%s ",(record1.dest_IP));
  58.                                 printf("%s ",(record1.s_port));
  59.                                 printf("%s ",(record1.d_port));
  60.                                 printf("%s ",(record1.range_disallow[0]));
  61.                                 printf("%s ",(record1.range_disallow[1]));
  62.                                 fflush(stdout);
  63.                                 //buff=malloc(sizeof(Record));
  64.                          }
  65.                     fclose(f2);
  66.                   }
  67.            else      
  68.                        printf("\ncould not open%s",errmsg);
  69.           return 0;
  70.          }
May 29 '07 #7

AdrianH
Expert 100+
P: 1,251
Don't write pointers to a file. It doesn't make sense. Tell me, what would happen if you read in a bunch of pointers using a different app? The pointers will not be valid in that app (if it is valid here it is barely valid).

Write the contents, not the pointer.

Anciant proverb: It is the fool who looks at the finger that is pointing at the moon. (or something like that ;)).


Adrian
May 29 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.