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

another fwrite() problem

P: n/a
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

Jan 6 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ib*********@gmail.com 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.
Jan 6 '06 #2

P: n/a
ib*********@gmail.com 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[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


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

Jan 6 '06 #3

P: n/a
tmp123 wrote:
ib*********@gmail.com 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[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


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

Sorry, sorry, sorry:
fwrite( d[i].i[j].name,... ) ??

Jan 6 '06 #4

P: n/a
J
ib*********@gmail.com 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[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


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

JC

Jan 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.