how to read fields of a struct from file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • liyankka
    New Member
    • Nov 2006
    • 7

    how to read fields of a struct from file

    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]
    Last edited by AdrianH; May 29 '07, 12:25 PM. Reason: Please use [code=c][/code] tags to improve readability. Also INDENT YOUR CODE!
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    Originally posted by liyankka
    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.

    /* 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_I P),&(record.des t_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_di sallow[0]),&(record.rang e_disallow[1]));

    buff=&record;
    fwrite(buff,siz eof(Record),1,f 1);
    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(buf f,sizeof(Record ),1,f2)>0)
    {
    record1=*((Reco rd*)buff);
    printf("%s %s %s %s",record1.sou rce_IP,record1. dest_IP,record1 .s_port, record1.d_port) ;
    }
    fclose(f2);
    }
    return 0;
    }
    U can use scanf for that.

    For more info check this article ;)


    Savage

    Comment

    • liyankka
      New Member
      • Nov 2006
      • 7

      #3
      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 .

      Comment

      • AdrianH
        Recognized Expert Top Contributor
        • Feb 2007
        • 1251

        #4
        Originally posted by liyankka
        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

        Comment

        • liyankka
          New Member
          • Nov 2006
          • 7

          #5
          Originally posted by AdrianH
          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.

          Comment

          • AdrianH
            Recognized Expert Top Contributor
            • Feb 2007
            • 1251

            #6
            Originally posted by liyankka
            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

            Comment

            • liyankka
              New Member
              • Nov 2006
              • 7

              #7
              Originally posted by AdrianH
              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.


              [code=c]#include "record.h"
              int main()
              {
              FILE * f1,*f2;
              Record record,record1;
              int i;
              void *buff;

              record.source_I P=(char*)calloc (16,sizeof(char ));
              record.dest_IP= (char*)calloc(1 6,sizeof(char)) ;
              record.s_port=( char*)calloc(4, sizeof(char));
              record.d_port=( char*)calloc(4, sizeof(char));
              record.range_di sallow[0]=(char*)calloc( 10,sizeof(char) );
              record.range_di sallow[1]=(char*)calloc( 10,sizeof(char) );

              record1.source_ IP=(char*)callo c(16,sizeof(cha r));
              record1.dest_IP =(char*)calloc( 16,sizeof(char) );
              record1.s_port= (char*)calloc(4 ,sizeof(char));
              record1.d_port= (char*)calloc(4 ,sizeof(char));
              record1.range_d isallow[0]=(char*)calloc( 10,sizeof(char) );
              record1.range_d isallow[1]=(char*)calloc( 10,sizeof(char) );

              f1=fopen("./conftemp.txt"," wb");
              if(f1!=NULL)
              {
              do{
              printf("\nenter source_IP and dest_IP \n");
              scanf("%s",(rec ord.source_IP)) ;
              scanf("%s",(rec ord.dest_IP));
              printf("\nenter source_port and dest_port\n");
              scanf("%s",(rec ord.s_port));
              scanf("%s",(rec ord.d_port));
              printf("\nenter start and end time of restricted period\n");
              scanf("%s",(rec ord.range_disal low[0]));
              scanf("%s",(rec ord.range_disal low[1]));

              buff=&record;
              fwrite(buff,siz eof(Record),1,f 1);
              fflush(f1);

              printf("\n more records? say 1 or 0 for yes /no:");
              scanf("%d",&i);
              } while(i==1);
              fclose(f1);
              }

              f2=fopen("./conftemp.txt"," rb");
              if(f2!=NULL)
              {
              while(fread(buf f,sizeof(Record ),1,f2)>0)
              {
              record1=*((Reco rd*)buff); //reads but repeats the last
              // record input to file instead of showing from the initial input???

              printf("\n");
              printf("%s ",(record1.sour ce_IP));
              printf("%s ",(record1.dest _IP));
              printf("%s ",(record1.s_po rt));
              printf("%s ",(record1.d_po rt));
              printf("%s ",(record1.rang e_disallow[0]));
              printf("%s ",(record1.rang e_disallow[1]));
              fflush(stdout);
              //buff=malloc(siz eof(Record));
              }
              fclose(f2);
              }
              else
              printf("\ncould not open%s",errmsg) ;
              return 0;
              }[/code]

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                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

                Comment

                Working...