Thread: Writing data to file

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    Writing data to file

    Actually Title is not clear enough i will try to ask on a example code
    Code:
    typedef struct{
    char name[10];
    int no;
    }TAM;
    
    typedef struct{
    char name[10];
    char ch;
    }HARF;
    
    
    typedef struct{
    TAM *i;
    CHAR *c;
    }DENEME;
    
    
    main(){
    FILE *f1;
    DENEME *d;
    d=(DENEME *) malloc(sizeof(DENEME)*2);
    int i;
    
    d[0].i=(TAM *) malloc(sizeof(TAM)*2);
    d[0].c=(HARF *) malloc(sizeof(HARF)*2);
    d[1].i=(TAM *) malloc(sizeof(TAM)*2);
    d[1].c=(HARF *) malloc(sizeof(HARF)*2);
    
    /* Every think ok for now  i can get info from user like  (d[0].i[0].name,d[0].i[0].no) */
    
    f1=fopen("c:\\deneme.txt","w+b");
    
    /* i just wnt to write d[i] to File*/
    for(i=0;i<2;i++){
    
    fwrite(d[i],sizeof(DENEME),1,f1); //if i try like this  there  is an error'cannot convert  '
    
    fwrite(&d[i],sizeof(DENEME),1,f1) // if i try like this  this time it writes the value of pointers 
    
    
    
    
    fwrite(d[i],sizeof(TAM)*2+sizeof(HARF)*2,1,f1)// finally i try this but it gives  error agin 
    }
    
    
    }
    im able to write like this
    fwrite((d+0)->i,sizeof(TAM),2,f1);
    fwrite((d+0)->c,sizeof(HARF),2,f1);
    but i doesnot help for my algorithms cause i want to read d[i] so that with one read i can have d[i].i[j].name

    hope im could explain my problem clearly..

    sorry for my english :P

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Where are all your #includes?

    2. main(){
    main returns an int. You should really get into the habit of being specific about your return types. The new C standard doesn't allow implicit types anymore.

    3. d=(DENEME *) malloc(sizeof(DENEME)*2);
    Dont cast the return result of malloc in C - see the FAQ

    4. int i;
    This is after a statement, C doesn't (yet) allow mixed declarations and statements.
    Move it above your malloc call.

    5. d[0].c=(HARF *) malloc(sizeof(HARF)*2);
    But c is a CHAR * (which isn't defined), not a HARF *

    > /* i just wnt to write d[i] to File*/
    Then it would be
    fwrite ( d[i].i, sizeof *d[i].i, 2, fp );
    fwrite ( d[i].c, sizeof *d[i].c, 2, fp );

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    sorry

    it should be like

    Code:
    typedef struct{
    TAM *i;
    HARF *c;
    }DENEME;
    
    thanx for other suggestions 
    
    but  i already did 
    fwrite ( d[i].i, sizeof *d[i].i, 2, fp );
    fwrite ( d[i].c, sizeof *d[i].c, 2, fp );
    
    but it isnot good solution for my algorthm
    if i write like this;  later while reading  i cant read  like
    fread(d[0],sizeof(DENEME),1,f1);
    i need to do this 
    hope im clear enough..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, you are being clear - but it isn't going to happen.
    Each pointer needs to be followed EXPLICITLY when dealing with the likes of fwrite, since it has no idea what is a pointer, nor how much it might point to.

    Lose all the pointers in the struct (and all but your first malloc), I can see no reason for them
    Code:
    typedef struct{
       TAM i;
       HARF c;
    }DENEME;
    Then you'll be able to
    fread(&d[0],sizeof(DENEME),1,f1);

  5. #5
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    the reason why i use pointer we dont know how many TAM or HARf we will use its up to user i mean he will write to promt and we will write according to that informations

    its like database if he writes 2 TAm whose name are PHone values 1234 and ID the values 11 it will be like

    d[0].i[o].name="Phone "
    d[0].i[o].no="1234 "


    d[0].i[1].name="ID "
    d[0].i[1].no="11 "

    if i write like u said

    fwrite ( d[i].i, sizeof *d[i].i, 2, fp );
    fwrite ( d[i].c, sizeof *d[i].c, 2, fp );

    it will write first i and then c and later when i need to get information abot someone i need to make confused calculating to read info of then but if i wirte like
    fwrite ( d[i], sizeof *d[i], 1, fp );
    with one read and without confusing calculations i can have all information about d[i]

    amm i right?

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    just to make my question clearly gues there is table like

    ---------Number(int) telephone(int) grade(char) ..
    d[o] ----- 1 ----- ----- 2345 ----- ----- A
    d[1] ----- 2 ----- ----- 1435 ----- ---- b
    d[2] ------ 3 ----- ----- 3455 ----- ----- c


    at first we dont know how many INT coloumb and how m any char coloumb thats why i use TAM *i and HARF *c

    and we dont know how many user(d[]) thats whyi use DENEME *p

    after i get info from user i will allocate *i and *c
    there will be another function add() and whenever it is called we will add d[]

    and i want to write every vertical line like d[] so that its easy to add&delete and searh and sort and....

    if i write with horisonal coloums(that i can be able to) it will be hard to search sort add delete new user


    maybe there is another solution for this algorythm .... but i find this way
    Last edited by SeyN; 01-06-2006 at 08:58 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Then for each block of memory which you malloc, and which can be variable in size, you need to also write this size information to the file, and read it back again.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct{
      char  name[10];
      int   no;
    }TAM;
    
    typedef struct{
      char  name[10];
      char  ch;
    }HARF;
    
    
    typedef struct{
      int   numTams;
      int   numHarfs;
      TAM   *tams;
      HARF  *harfs;
    }DENEME;
    
    int main(void)
    {
      int     numDenemes = 2; /* some number */
      DENEME  *d = malloc ( numDenemes * sizeof *d );
      FILE    *fp;
      int     i;
    
      // an initial database of some sort
      d[0].numTams  = 1;
      d[0].tams     = malloc(sizeof(TAM)*d[0].numTams);
      d[0].numHarfs = 2;
      d[0].harfs    = malloc(sizeof(HARF)*d[0].numHarfs);
      d[1].numTams  = 3;
      d[1].tams     = malloc(sizeof(TAM)*d[1].numTams);
      d[1].numHarfs = 4;
      d[1].harfs    = malloc(sizeof(HARF)*d[1].numHarfs);
    
      // writing
      fwrite ( &numDenemes, sizeof numDenemes, 1, fp ); /* total number */
      for ( i = 0 ; i < numDenemes ; i++ ) {
        /* write out how many of each record we have */
        fwrite ( &d[i].numTams,   sizeof d[i].numTams,  1, fp );
        fwrite ( &d[i].numHarfs,  sizeof d[i].numHarfs, 1, fp );
        /* write out the records */
        fwrite ( d[i].tams,   sizeof *d[i].tams,  d[i].numTams,  fp );
        fwrite ( d[i].harfs,  sizeof *d[i].harfs, d[i].numHarfs, fp );
      }
    
      // reading (assuming nothing has been malloc'ed yet)
      fread ( &numDenemes, sizeof numDenemes, 1, fp ); /* total number */
      d = malloc ( numDenemes * sizeof *d );
      for ( i = 0 ; i < numDenemes ; i++ ) {
        /* read in how many of each record we have */
        fread ( &d[i].numTams,  sizeof d[i].numTams,  1, fp );
        fread ( &d[i].numHarfs, sizeof d[i].numHarfs, 1, fp );
    
        /* allocate space for the records we're about to read */
        d[i].tams  = malloc(sizeof(TAM)*d[i].numTams);
        d[i].harfs = malloc(sizeof(TAM)*d[i].numHarfs);
    
        /* read in the records */
        fread ( d[i].tams,  sizeof *d[i].tams,  d[i].numTams,  fp );
        fread ( d[i].harfs, sizeof *d[i].harfs, d[i].numHarfs, fp );
      }
    
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    first of all really thanx for your interest

    actually im plannng to store info like
    int numTams;
    int numHarfs;
    numDenemes;
    in another file because of futurer properties (teher may be lots of file so its not logical to open ever file and get the information)
    any way


    im sory if im wrong but
    if we read like
    fread ( d[i].tams, sizeof *d[i].tams, d[i].numTams, fp );
    printf("%s \n ",d[1].tams[0].name);
    it will give us some strage characters that pointer has

    if we read like
    fread ( &d[i].tams, sizeof *d[i].tams, d[i].numTams, fp );
    tahts allright but go and look to your file there will be again the pointers i mean when we close the program and when we just try to read data like

    f1=fopen("c:\\deneme.txt","r+b");

    fread(d[0].tams,sizeof *d[i].tams,1,f1);

    printf("%s \n ",d[3].i[0].name);
    printf("%d \n",d[3].i[0].no);

    there will be again only strange chars

    am i doing right ?

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    im really confused

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ok, real example, and some output.
    Error checks on malloc, fopen and fread have been omitted for brevity.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct {
      char  name[10];
      int   no;
    } TAM;
    
    typedef struct {
      char  name[10];
      char  ch;
    } HARF;
    
    typedef struct {
      int   numTams;
      int   numHarfs;
      TAM   *tams;
      HARF  *harfs;
    } DENEME;
    
    DENEME *allocDenemes ( int numDenemes ) {
      int      i, j;
      DENEME  *d = malloc ( numDenemes * sizeof *d );
      for ( i = 0 ; i < numDenemes ; i++ ) {
        d[i].numTams = i + 1;
        d[i].numHarfs= i + 2;
        d[i].tams    = malloc ( d[i].numTams * sizeof *d[i].tams );
        d[i].harfs   = malloc ( d[i].numHarfs* sizeof *d[i].harfs );
        for ( j = 0 ; j < d[i].numTams ; j++ ) {
          sprintf( d[i].tams[j].name, "Name %d", i + j );
          d[i].tams[j].no = i + j;
        }
        for ( j = 0 ; j < d[i].numHarfs ; j++ ) {
          sprintf( d[i].harfs[j].name, "Name %d", i + j );
          d[i].harfs[j].ch = i + j + 'a';
        }
      }
      return d;
    }
    
    void freeDenemes ( DENEME *d, int numDenemes ) {
      int i;
      for ( i = 0 ; i < numDenemes; i++ ) {
        free ( d[i].tams );
        free ( d[i].harfs );
      }
      free ( d );
    }
    
    void printDenemes ( DENEME *d, int numDenemes ) {
      int i, j;
      for ( i = 0 ; i < numDenemes; i++ ) {
        printf( "TAMS for %d\n", i );
        for ( j = 0 ; j < d[i].numTams ; j++ ) {
          printf ( "-- Name=%s, no=%d\n",
                   d[i].tams[j].name, d[i].tams[j].no );
        }
        printf( "HARFS for %d\n", i );
        for ( j = 0 ; j < d[i].numHarfs ; j++ ) {
          printf ( "-- Name=%s, ch=%c\n",
                   d[i].harfs[j].name, d[i].harfs[j].ch );
        }
      }
    }
    
    void saveDenemes ( DENEME *d, int numDenemes, const char *filename ) {
      int i;
      FILE *fp = fopen ( filename, "wb" );
    
      fwrite ( &numDenemes, sizeof numDenemes, 1, fp ); /* total number */
      for ( i = 0 ; i < numDenemes ; i++ ) {
        /* write out how many of each record we have */
        fwrite ( &d[i].numTams,   sizeof d[i].numTams,  1, fp );
        fwrite ( &d[i].numHarfs,  sizeof d[i].numHarfs, 1, fp );
        /* write out the records */
        fwrite ( d[i].tams,   sizeof *d[i].tams,  d[i].numTams,  fp );
        fwrite ( d[i].harfs,  sizeof *d[i].harfs, d[i].numHarfs, fp );
      }
      fclose ( fp );
    }
    
    DENEME *loadDenemes ( int *numDenemes, const char *filename ) {
      int i;
      FILE *fp = fopen ( filename, "rb" );
      DENEME  *d;
    
      fread ( numDenemes, sizeof *numDenemes, 1, fp ); /* total number */
      d = malloc ( *numDenemes * sizeof *d );
      for ( i = 0 ; i < *numDenemes ; i++ ) {
        /* read in how many of each record we have */
        fread ( &d[i].numTams,  sizeof d[i].numTams,  1, fp );
        fread ( &d[i].numHarfs, sizeof d[i].numHarfs, 1, fp );
    
        /* allocate space for the records we're about to read */
        d[i].tams  = malloc(sizeof(TAM)*d[i].numTams);
        d[i].harfs = malloc(sizeof(TAM)*d[i].numHarfs);
    
        /* read in the records */
        fread ( d[i].tams,  sizeof *d[i].tams,  d[i].numTams,  fp );
        fread ( d[i].harfs, sizeof *d[i].harfs, d[i].numHarfs, fp );
      }
      fclose ( fp );
      return d;
    }
    
    int main ( void ) {
      const char *filename = "savedata.dat";
      int         numDenemes = 2; /* some number */
      DENEME     *d;
    
      printf( "Initialise, print it, save it and free\n" );
      d = allocDenemes ( numDenemes );
      printDenemes ( d, numDenemes );
      saveDenemes ( d, numDenemes, filename );
      freeDenemes ( d, numDenemes );
    
      printf( "\nLoad it, print it and free\n" );
      d = loadDenemes ( &numDenemes, filename );
      printDenemes ( d, numDenemes );
      freeDenemes ( d, numDenemes );
    
      return 0;
    }
    
    
    $ ./a.out
    Initialise, print it, save it and free
    TAMS for 0
    -- Name=Name 0, no=0
    HARFS for 0
    -- Name=Name 0, ch=a
    -- Name=Name 1, ch=b
    TAMS for 1
    -- Name=Name 1, no=1
    -- Name=Name 2, no=2
    HARFS for 1
    -- Name=Name 1, ch=b
    -- Name=Name 2, ch=c
    -- Name=Name 3, ch=d
    
    Load it, print it and free
    TAMS for 0
    -- Name=Name 0, no=0
    HARFS for 0
    -- Name=Name 0, ch=a
    -- Name=Name 1, ch=b
    TAMS for 1
    -- Name=Name 1, no=1
    -- Name=Name 2, no=2
    HARFS for 1
    -- Name=Name 1, ch=b
    -- Name=Name 2, ch=c
    -- Name=Name 3, ch=d

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Thanks for your interest & help Salem your code helped me alot hope someother friends also should have lookto this code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM