How to assign File data to a Structure?

Discussion in 'C' started by Shilpa1, Apr 10, 2008.

  1. Shilpa1

    Shilpa1 New Member

    Nov 8, 2007
    Likes Received:
    Trophy Points:
    Here,Iam reading the File Data to a string.Now I want to store the FileData to a structure.How this is posible?pls help me to solve this.

    typedef struct _S_
         void          *data;
         struct _S_   *next;
     FILE *fp = NULL;
     //Open the File
     fp = fopen("MyFile.txt","r");
     char *buffer;
     long lSize;
     size_t result;
       fseek (fp , 0 , SEEK_END);
       lSize = ftell (fp);
      rewind (fp);
      // allocate memory to contain the whole file:
      buffer = (char*) malloc (sizeof(char)*lSize);
      if (buffer == NULL) 
       puts ("Memory error",stderr); 
       exit (2);
    // copy the file into the buffer:
       result = fread (buffer,sizeof(char),lSize,fp);
       if (result != lSize) 
        fputs ("Reading error",stderr); 
        exit (3);
    //[I]Here I want to store the buffer to the Structure[B] ( void * data)[/B][/I] 

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice