ورود

View Full Version : هر کی میدونه زود جواب بده



na30m
دوشنبه 14 دی 1383, 22:34 عصر
سلام.یه پروژه برای درس ذخیره وبازیابی اطلاعات استادمون گفته در مورد اینکه اطلاعات دانشجو را در فایلی ذخیره کنیم و در هنگام نیاز بتوانیم این اطلاعات را آپدیت ،حذف کنیم و یا رکورد جدیدی اضافه کنیم.در نوشتن و الگوریتم برنامه مشکلی ندارم فقط مشکل اصلی اینجاست که در فایل مثلاinfo.datکه برنامه را ذخیره کرده ایم اطلاعات دانشجویان در آن نمی ماند و وقتی فولدر info را باز میکینیم خالی است علیرغم اینکه برنامه تا حدودی اجرا میشود (این برنامه را به زبان ++cنوشته ام)اگر لطف کنید و هر چه سریعتر به سوال من پاسخ دهید بسیار متشکر می شوم.موفق باشید(در ضمن قسمتی از برنامه مثل inputبه صورت زیر میباشد ولی در فایل مذکور هیچی ذخیره نکرده است
void input(void
{struct stu st;
int i;
FILE *fp;
fp=fopen("info.dat","wb");
if(fp==NULL)
{cout<<"can not open file";
exit(0);}
for(i=0;i<n;i++)
{cout<<"enter name,num,id,diploma,sal virud,address,reshteh:";
cin>>st.name>>st.num>>st.id>>st.diploma>>st.salvorud>>st.address>>st.reshteh;
fwrite(&st,sizeof(st),1,fp);
}
fclose(fp);
cout<<"record="<<i;
getch();
}

Aminm_666
جمعه 18 دی 1383, 15:38 عصر
تو fopen از w برای نوع دسترسی استفاده کردی. معلومه که نمیمونه چون داری بهش میگی که همین کارو بکنه. برای خواندن و نوشتن و اضافه کردن به انتهای فایل باید از کاراکترهای مخصوص به خودش استفاده کرد.
عین کاراکترها و توضیحاتشون رو از MSDN کپی میکنم:

<span dir=ltr>"r"

Opens for reading. If the file does not exist or cannot be found, the fopen call fails.

"w"

Opens an empty file for writing. If the given file exists, its contents are destroyed.

"a"

Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn’t exist.

"r+"

Opens for both reading and writing. (The file must exist.)

"w+"

Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

"a+"

Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn’t exist.
</span>

Pars Giga
یک شنبه 15 خرداد 1384, 09:40 صبح
rb wb ab
برای باینری ها