Jump to content


Friends, we are happy to invite you to our NEW Global Kaspersky Club! Please follow this link www.kasperskyclub.com , sign-in and enjoy our new platform!


Photo
- - - - -

the boring C language


  • Please log in to reply
2 replies to this topic

#1 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 18 July 2007 - 06:46 PM

Hello,all!

The new C language problem have puzzled me for days. That's about fwrite() (the function is used to output the data from EMS to HDD agglomerately), and the return of this function is the value of the count according to my book.

So these codes :

#include<stdio.h>
#define SIZE 4
char FILENAME[18];


struct record
{
int num;

float money;
}list[SIZE];





void insert()
{
int i;
float temp;
list[0].num=0;

for(i=0;i<SIZE;i++)
{
printf("Please input your data!\n");
scanf("%f",&temp);
list[i].money=temp;
list[i].num=i+1;
}
}

void display()
{
int i;

for(i=0;i<SIZE;i++)
printf("%-4d%-4f\n",list[i].num,list[i].money);

}

main()
{
int i;
FILE *fp;
printf("Please enter the file name that you want to creat\n");
scanf("%s",FILENAME);

if((fp=fopen(FILENAME,"wb"))==NULL)
{
printf("The system can't open the file\n");
printf("Please press any to stop the system!\n");
getch();
exit(0);
}
else printf("the file is created successfully!\n");
printf("Then you should insert some data\n");

insert();
display();

for(i=0;i<SIZE;i++)
if(fwrite(&list[i],sizeof(struct record),1,fp)!=1);
printf("file write error\n");



fclose(fp);
getch();
}



the result of the program is in the affix. (Sorry, I don't konw how to put the screenshot here directly)


Why, the result of that always is file write error. You konw, it shows the function of fwrite is transfered wrongly.

Why the error will happen?

Thanks for you paying some time attention this problem , thanks in advance.

#2 Lucian Bara

Lucian Bara

    Jedi Master

  • Global Moderators
  • PipPipPipPipPip
  • 912 posts

Posted 18 July 2007 - 06:58 PM

what error?
ps you have to include conio if you use getch()
and for exit either stdlib.h or process.h
Intel Core 2 Duo E4500 @ 2,2GHz, Msi Asus P5PL2-E, 2048MB-DDR2 (2*1024), Leadtek PX6600 256MB, Teac DV-W516GA, Leadtek tv2000 xp Expert, HDD 200GB*2 (sata/sata2)

#3 ruyun

ruyun

    Newbie

  • Members
  • 16 posts

Posted 18 July 2007 - 07:08 PM

what error?
ps you have to include conio if you use getch()
and for exit either stdlib.h or process.h



Oh,no. I followed your ideas to add the inculde files is useless.

The error is about fwrite(), follow the book, if the sentence :printf("flie write error") was run, it shows the fwrite() is used wrongly.