another fwrite() problem

Discussion in 'C Programming' started by ibrahimover@gmail.com, Jan 6, 2006.

  1. Guest

    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 to File*/
    for(i=0;i<2;i++){

    fwrite(d,sizeof(DENEME),1,f1); //if i try like this there is an
    error'cannot convert '

    fwrite(&d,sizeof(DENEME),1,f1) // if i try like this this time it
    writes the value of pointers




    fwrite(d,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 so that
    with one read i can have d.i[j].name

    hope im could explain my problem clearly..

    sorry for my english :p
    , Jan 6, 2006
    #1
    1. Advertising

  2. wrote:

    > d=(DENEME *) malloc(sizeof(DENEME)*2);


    http://c-faq.com/malloc/mallocnocast.html

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Jan 6, 2006
    #2
    1. Advertising

  3. tmp123 Guest

    wrote:
    > 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 to File*/
    > for(i=0;i<2;i++){
    >
    > fwrite(d,sizeof(DENEME),1,f1); //if i try like this there is an
    > error'cannot convert '
    >
    > fwrite(&d,sizeof(DENEME),1,f1) // if i try like this this time it
    > writes the value of pointers
    >
    >
    >
    >
    > fwrite(d,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 so that
    > with one read i can have d.i[j].name




    fwrite( &d.i[j].name,... ) ??
    tmp123, Jan 6, 2006
    #3
  4. tmp123 Guest

    tmp123 wrote:
    > wrote:
    > > 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 to File*/
    > > for(i=0;i<2;i++){
    > >
    > > fwrite(d,sizeof(DENEME),1,f1); //if i try like this there is an
    > > error'cannot convert '
    > >
    > > fwrite(&d,sizeof(DENEME),1,f1) // if i try like this this time it
    > > writes the value of pointers
    > >
    > >
    > >
    > >
    > > fwrite(d,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 so that
    > > with one read i can have d.i[j].name

    >
    >
    >
    > fwrite( &d.i[j].name,... ) ??



    Sorry, sorry, sorry:
    fwrite( d.i[j].name,... ) ??
    tmp123, Jan 6, 2006
    #4
  5. J Guest

    wrote:
    > 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 to File*/
    > for(i=0;i<2;i++){
    >
    > fwrite(d,sizeof(DENEME),1,f1); //if i try like this there is an
    > error'cannot convert '
    >
    > fwrite(&d,sizeof(DENEME),1,f1) // if i try like this this time it
    > writes the value of pointers
    >
    >
    >
    >
    > fwrite(d,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 so that
    > with one read i can have d.i[j].name
    >
    > hope im could explain my problem clearly..
    >
    > sorry for my english :p


    the error results because d is not a pointer however the address of
    d is a pointer.

    JC
    J, Jan 6, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nescio
    Replies:
    0
    Views:
    444
    nescio
    Dec 21, 2005
  2. nescio

    fwrite() question

    nescio, Dec 21, 2005, in forum: HTML
    Replies:
    0
    Views:
    356
    nescio
    Dec 21, 2005
  3. Brady

    problem using fread, fwrite, and fsetpos

    Brady, Jul 17, 2003, in forum: C Programming
    Replies:
    8
    Views:
    930
    Dave Thompson
    Jul 21, 2003
  4. Replies:
    6
    Views:
    732
    Keith Thompson
    Feb 9, 2005
  5. Jeff
    Replies:
    16
    Views:
    1,428
    Kenny McCormack
    Jun 9, 2008
Loading...

Share This Page