...StacK...
یک شنبه 11 مرداد 1388, 05:59 صبح
سلام.
فرض کنید یه ساختمان به صورت زیر داریم:
struct x{
char name[20];
int ,x,y;
}list[20];
int main(void){
....
....
...
return 0;
}
void x (void)
{
...
*(list[5].name)='a';
*list[5].name='a';
حالا اولین ابهامی که دارم در دو خط اخر از برنامه هست....
من فکر میکنم دو خط اخر برنامه یکی هست و بود یا نبود اون پرانتز فرقی نمیکنه....
ایا این موضوع درسته؟چون ما در هر دو صورت به محتوای ادرس name میرسیم
یعنی اولین خونه از ارایه از متغیر ساختمان لیست.
حالا اگه بنویسیم :
list[5].name[0]='a';
من بازم نتیجه میگیرم که این خط هم با اون دو خط هیچ فرقی نداره ,درسته؟؟؟
-----------------------------------------------------
سوال بعدی :
میگم این قضیه حالت های باز شدن یک فایل چیه؟؟؟مثلا باینری و یا متنی
منظور از باینری مگه فایل های اجرایی نیست؟؟؟ پس چرا ما از این حالت (b) برای فایل
های متنی هم استفاده میکنیم؟؟؟؟مثل نوشتن یک متن در یک فایل و یا باز کردن یک فایل
متنی موجود در حالت باینری...؟؟؟
-----------------------------------------------------
سوال بعدی:به این کد دقت کنید:xx
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define SIZE 100
struct student {
char name[10];
int stno,sex;
float grade;
}list[SIZE];
int menu(void);
void init_list(void),enter(void);
void display(void),save(void);
void load(void);
int main(void)
{
init_list();
for(;;){
switch(menu()){
case 'e':enter();break;
case 'd':display();break;
case 's':save();break;
case 'l':load();break;
case 'q':exit(0);
}//end of switch
}//end of for
}
//******************************
void enter(void)
{
register int i;
int row;
char numstr[10];
for(i=0;i<SIZE;i++)
if(!(*list[i].name))
break;
if(i==SIZE){
printf("\n list is full press a key...");
getch();
}
clrscr();
gotoxy(10,2);
puts("<<INPUT DATA>>");
gotoxy(1,3);
printf(" name stno gread sex(1,2)");
gotoxy(1,4);
puts("-------- ------- ------ --------");
row=5;
for(;;){
gotoxy(1,row);
gets(list[i].name);
if(!(*list[i].name)) break;
gotoxy(16,row);
gets(numstr);
list[i].stno=atoi(numstr);
gotoxy(28,row);
gets(numstr);
list[i].grade=atof(numstr);
gotoxy(40,row);
gets(numstr);
list[i].sex=atoi(numstr);
row ++;
i++;
}
}
//****************
void init_list(void)
{
register int t;
for(t=0;t<SIZE;t++)
*list[t].name='\0';
}
//********************
int menu(void)
{
char s[10];
clrscr();
do{
gotoxy(20,4);
printf("E) enter data ");
gotoxy(20,6);
printf("D) display on screen");
gotoxy(20,8);
printf("L) load file");
gotoxy(20,10);
printf("S) save in file");
gotoxy(20,12);
printf("Q) quit");
gotoxy(20,14);
printf("enter your select:");
gets(s);
}while(!strchr("edlsq",tolower(*s)));
return tolower(*s);
}
//*********************************
void display(void)
{
register int t;
int row;
clrscr();
gotoxy(10,2);
puts("<< OUTPUT DATA >>");
gotoxy(1,3);
printf("----------- ------- ------ -----------");
row=5;
for(t=0;t<SIZE;t++)
if(*list[t].name){
gotoxy(1,row);
printf("%s",list[t].name);
gotoxy(12,row);
printf("%d",list[t].stno);
gotoxy(20,row);
printf("%5.2f",list[t].grade);
gotoxy(30,row);
printf("%d",list[t].sex);
row++;
}//end of if
gotoxy(5,row+2);
printf("press a key ...");
getch();
}
//*******************************
void save(void)
{
FILE *fp;
register int i;
fp=fopen("st.txt","w(wt)");
if(!fp){
printf("\n cannot open file press a key...");
getch();
return;
}
for(i=0;i<SIZE;i++)
if(*list[i].name)
fwrite(&list[i],sizeof(struct student),1,fp);//end of if /end of for
fclose(fp);
clrscr();
gotoxy(20,10);
printf("data saved. prees a key.");
getch();
}
//*****************
void load(void)
{
FILE *fp;
register int i;
fp=fopen("st.txt","r+(r+t)");
if(!fp){
printf("\n cannot open file. press a key...");
getch();
return;
}
init_list();
for(i=0;i<SIZE;i++){
fread(&list[i],sizeof(struct student),1,fp);
if(feof(fp)){
clrscr();
gotoxy(20,10);
printf("data loaded.press a key.");
getch();
fclose(fp);
return;
}//end of if
}
fclose(fp);
}
بعد از اجرای فایل یه سری داده به برنامه بدید و در فایل ذخیره کنید, و بعد فایل رو ببینید
بعضی از قسمت های رشته به صورت نا معلوم هست ,یعنی یه حالت هش شده مانندی
مثلا محتوای فایلی که باید به صورت زیر باشه:
ali 2222 20 1
barnamenevis 1111 20 1
به صورت زیر هست:
ali ق AbarnamenevW A
این کد یه مثال از کتاب c قمی هست ,که البته از روی کتاب دوباره نوشتم و یه سری
کم و کاستی هاشو درست کردم "نظیر اینکه در کد اصلی بعد از بازکردن فایل اونا نبسته بود
و یا در مد باینری باز کرده بود"... با این حال در کد اصلی هم این مشکل وجود داره:
اینم کد اصلی:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
struct student {
char name [10] ;
int stno, sex ;
float grade ;
} list[SIZE] ;
int menu(void) ;
void init_list(void) , enter(void) ;
void display(void) , save(void) ;
void load(void) ;
int main()
{
init_list();
for(;;) {
switch(menu()) {
case 'e': enter(); break;
case 'd': display(); break ;
case 's' : save() ; break;
case 'l' : load() ; break ;
case 'q': exit(0) ;
}//end of switch
} //end of for
}
//**********************
void init_list(void)
{
register int t;
for (t = 0 ; t < SIZE ; t++)
*(list[t].name) = '\0' ;
}
//***********************
void enter(void)
{
register int i ;
int row ;
char numstr[10];
for(i = 0; i < SIZE; i++)
if (!(*list[i].name))
break ;
if(i == SIZE) {
printf("\n list is full press a key ...") ;
getch() ;
}
clrscr() ;
gotoxy(10, 2) ;
puts("<< INPUT DATA >>");
gotoxy(1, 3) ;
printf(" name stno gread sex(1, 2)" ) ;
gotoxy(1,4) ;
puts (" ------- ----- ------- -------") ;
row = 5 ;
for(;;) {
gotoxy(1, row);
gets(list[i].name);
if(! (*list[i].name))
break ;
gotoxy(16, row);
gets(numstr) ;
list[i].stno = atoi(numstr) ;
gotoxy(28, row) ;
gets(numstr) ;
list[i].grade = atof(numstr) ;
gotoxy(40, row) ;
gets(numstr) ;
list[i].sex = atoi(numstr) ;
row ++ ;
i ++ ;
}
}
//***********************
void display(void)
{
register int t ;
int row ;
clrscr() ;
gotoxy(10, 2) ;
puts("<< OUTPUT DATA >> ") ;
gotoxy(1, 3) ;
printf(" name stno grade sex(1,2)") ;
gotoxy(1, 4);
printf("------- ---- ----- --------") ;
row = 5 ;
for(t = 0 ; t < SIZE ; t++)
if(*list[t].name) {
gotoxy(1, row) ;
printf("%s", list[t].name);
gotoxy(12, row) ;
printf("%d", list[t].stno);
gotoxy(20, row) ;
printf("%5.2f", list[t].grade) ;
gotoxy(30, row) ;
printf("%d", list[t].sex) ;
row ++ ;
} //end of if
gotoxy(5, row+2);
printf(" press a key ...");
getch() ;
}
//***********************
void save(void)
{
FILE *fp ;
register int i ;
fp = fopen("st", "wb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
return ;
}
for(i = 0 ; i < SIZE ; i++)
if(*list[i].name)
fwrite(&list[i], sizeof(struct student), 1, fp) ;
clrscr() ;
gotoxy(20, 10) ;
printf("data saved.press a key.");
getch() ;
}
//************************
void load(void)
{
FILE *fp ;
register int i ;
fp = fopen("st", "rb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
return ;
}
init_list() ;
for(i = 0 ; i < SIZE ; i++) {
fread(&list[i], sizeof(struct student), 1, fp);
if(feof(fp)) {
clrscr() ;
gotoxy(20,10) ;
printf("data loaded.press a key.");
getch() ;
return ;
}//end of if
}
}
//*****************
int menu(void)
{
char s[10] ;
clrscr();
do {
gotoxy(20, 4);
printf("E) enter data ");
gotoxy(20, 6);
printf("D) display on screen");
gotoxy(20, 8);
printf("L) load file");
gotoxy(20, 10);
printf("S) save in file");
gotoxy(20, 12);
printf("Q) quit");
gotoxy(20, 14);
printf("enter your select:");
gets(s) ;
} while(!strchr("edlsq", tolower(*s)));
return tolower(*s) ;
}
---------------------------------
سوال بعدیم:xx
فرض کنید این خط کد به صورت عمومی (در بالای main) تعریف میشه....
FILE *fopen();
منظور از این خط چیه؟؟ نه به اشاره گر فایل میخوره و نه ...پس چیه....
این کتاب قمی ادما روانی میکنه....برداشته یه مثال 300 خطی نوشته ...
حتی یک دونه متغیر رو هم توضیح نداده بعد تو قسمت بعد هم میگه:
خب در قسمت قبل مسئله فایل های ترتیبی برسی شد...بر هرچی دروغ گو هست لعنت.
------البته از یه کتاب انگلیسی به اسم:
C Primer Plus, Fifth EditionBy Stephen Prata دارم میخونم ولی خب اولاش هستم....واسه توضیح یه متغیر 100 شکل کشیده و 200خط
توضیح داده--به این میگم کتاب....
ولی کتاب اون قمی رو هم شروع کردم ....نمیتونم نیمه کار رهاش کنم...
پس مجبورم مشکلاتم رو اینجا مطرح کنم ....امیدوارم در حد توان راهنماییم کنید...
ممنونم.:قلب::بوس:
فرض کنید یه ساختمان به صورت زیر داریم:
struct x{
char name[20];
int ,x,y;
}list[20];
int main(void){
....
....
...
return 0;
}
void x (void)
{
...
*(list[5].name)='a';
*list[5].name='a';
حالا اولین ابهامی که دارم در دو خط اخر از برنامه هست....
من فکر میکنم دو خط اخر برنامه یکی هست و بود یا نبود اون پرانتز فرقی نمیکنه....
ایا این موضوع درسته؟چون ما در هر دو صورت به محتوای ادرس name میرسیم
یعنی اولین خونه از ارایه از متغیر ساختمان لیست.
حالا اگه بنویسیم :
list[5].name[0]='a';
من بازم نتیجه میگیرم که این خط هم با اون دو خط هیچ فرقی نداره ,درسته؟؟؟
-----------------------------------------------------
سوال بعدی :
میگم این قضیه حالت های باز شدن یک فایل چیه؟؟؟مثلا باینری و یا متنی
منظور از باینری مگه فایل های اجرایی نیست؟؟؟ پس چرا ما از این حالت (b) برای فایل
های متنی هم استفاده میکنیم؟؟؟؟مثل نوشتن یک متن در یک فایل و یا باز کردن یک فایل
متنی موجود در حالت باینری...؟؟؟
-----------------------------------------------------
سوال بعدی:به این کد دقت کنید:xx
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define SIZE 100
struct student {
char name[10];
int stno,sex;
float grade;
}list[SIZE];
int menu(void);
void init_list(void),enter(void);
void display(void),save(void);
void load(void);
int main(void)
{
init_list();
for(;;){
switch(menu()){
case 'e':enter();break;
case 'd':display();break;
case 's':save();break;
case 'l':load();break;
case 'q':exit(0);
}//end of switch
}//end of for
}
//******************************
void enter(void)
{
register int i;
int row;
char numstr[10];
for(i=0;i<SIZE;i++)
if(!(*list[i].name))
break;
if(i==SIZE){
printf("\n list is full press a key...");
getch();
}
clrscr();
gotoxy(10,2);
puts("<<INPUT DATA>>");
gotoxy(1,3);
printf(" name stno gread sex(1,2)");
gotoxy(1,4);
puts("-------- ------- ------ --------");
row=5;
for(;;){
gotoxy(1,row);
gets(list[i].name);
if(!(*list[i].name)) break;
gotoxy(16,row);
gets(numstr);
list[i].stno=atoi(numstr);
gotoxy(28,row);
gets(numstr);
list[i].grade=atof(numstr);
gotoxy(40,row);
gets(numstr);
list[i].sex=atoi(numstr);
row ++;
i++;
}
}
//****************
void init_list(void)
{
register int t;
for(t=0;t<SIZE;t++)
*list[t].name='\0';
}
//********************
int menu(void)
{
char s[10];
clrscr();
do{
gotoxy(20,4);
printf("E) enter data ");
gotoxy(20,6);
printf("D) display on screen");
gotoxy(20,8);
printf("L) load file");
gotoxy(20,10);
printf("S) save in file");
gotoxy(20,12);
printf("Q) quit");
gotoxy(20,14);
printf("enter your select:");
gets(s);
}while(!strchr("edlsq",tolower(*s)));
return tolower(*s);
}
//*********************************
void display(void)
{
register int t;
int row;
clrscr();
gotoxy(10,2);
puts("<< OUTPUT DATA >>");
gotoxy(1,3);
printf("----------- ------- ------ -----------");
row=5;
for(t=0;t<SIZE;t++)
if(*list[t].name){
gotoxy(1,row);
printf("%s",list[t].name);
gotoxy(12,row);
printf("%d",list[t].stno);
gotoxy(20,row);
printf("%5.2f",list[t].grade);
gotoxy(30,row);
printf("%d",list[t].sex);
row++;
}//end of if
gotoxy(5,row+2);
printf("press a key ...");
getch();
}
//*******************************
void save(void)
{
FILE *fp;
register int i;
fp=fopen("st.txt","w(wt)");
if(!fp){
printf("\n cannot open file press a key...");
getch();
return;
}
for(i=0;i<SIZE;i++)
if(*list[i].name)
fwrite(&list[i],sizeof(struct student),1,fp);//end of if /end of for
fclose(fp);
clrscr();
gotoxy(20,10);
printf("data saved. prees a key.");
getch();
}
//*****************
void load(void)
{
FILE *fp;
register int i;
fp=fopen("st.txt","r+(r+t)");
if(!fp){
printf("\n cannot open file. press a key...");
getch();
return;
}
init_list();
for(i=0;i<SIZE;i++){
fread(&list[i],sizeof(struct student),1,fp);
if(feof(fp)){
clrscr();
gotoxy(20,10);
printf("data loaded.press a key.");
getch();
fclose(fp);
return;
}//end of if
}
fclose(fp);
}
بعد از اجرای فایل یه سری داده به برنامه بدید و در فایل ذخیره کنید, و بعد فایل رو ببینید
بعضی از قسمت های رشته به صورت نا معلوم هست ,یعنی یه حالت هش شده مانندی
مثلا محتوای فایلی که باید به صورت زیر باشه:
ali 2222 20 1
barnamenevis 1111 20 1
به صورت زیر هست:
ali ق AbarnamenevW A
این کد یه مثال از کتاب c قمی هست ,که البته از روی کتاب دوباره نوشتم و یه سری
کم و کاستی هاشو درست کردم "نظیر اینکه در کد اصلی بعد از بازکردن فایل اونا نبسته بود
و یا در مد باینری باز کرده بود"... با این حال در کد اصلی هم این مشکل وجود داره:
اینم کد اصلی:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 100
struct student {
char name [10] ;
int stno, sex ;
float grade ;
} list[SIZE] ;
int menu(void) ;
void init_list(void) , enter(void) ;
void display(void) , save(void) ;
void load(void) ;
int main()
{
init_list();
for(;;) {
switch(menu()) {
case 'e': enter(); break;
case 'd': display(); break ;
case 's' : save() ; break;
case 'l' : load() ; break ;
case 'q': exit(0) ;
}//end of switch
} //end of for
}
//**********************
void init_list(void)
{
register int t;
for (t = 0 ; t < SIZE ; t++)
*(list[t].name) = '\0' ;
}
//***********************
void enter(void)
{
register int i ;
int row ;
char numstr[10];
for(i = 0; i < SIZE; i++)
if (!(*list[i].name))
break ;
if(i == SIZE) {
printf("\n list is full press a key ...") ;
getch() ;
}
clrscr() ;
gotoxy(10, 2) ;
puts("<< INPUT DATA >>");
gotoxy(1, 3) ;
printf(" name stno gread sex(1, 2)" ) ;
gotoxy(1,4) ;
puts (" ------- ----- ------- -------") ;
row = 5 ;
for(;;) {
gotoxy(1, row);
gets(list[i].name);
if(! (*list[i].name))
break ;
gotoxy(16, row);
gets(numstr) ;
list[i].stno = atoi(numstr) ;
gotoxy(28, row) ;
gets(numstr) ;
list[i].grade = atof(numstr) ;
gotoxy(40, row) ;
gets(numstr) ;
list[i].sex = atoi(numstr) ;
row ++ ;
i ++ ;
}
}
//***********************
void display(void)
{
register int t ;
int row ;
clrscr() ;
gotoxy(10, 2) ;
puts("<< OUTPUT DATA >> ") ;
gotoxy(1, 3) ;
printf(" name stno grade sex(1,2)") ;
gotoxy(1, 4);
printf("------- ---- ----- --------") ;
row = 5 ;
for(t = 0 ; t < SIZE ; t++)
if(*list[t].name) {
gotoxy(1, row) ;
printf("%s", list[t].name);
gotoxy(12, row) ;
printf("%d", list[t].stno);
gotoxy(20, row) ;
printf("%5.2f", list[t].grade) ;
gotoxy(30, row) ;
printf("%d", list[t].sex) ;
row ++ ;
} //end of if
gotoxy(5, row+2);
printf(" press a key ...");
getch() ;
}
//***********************
void save(void)
{
FILE *fp ;
register int i ;
fp = fopen("st", "wb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
return ;
}
for(i = 0 ; i < SIZE ; i++)
if(*list[i].name)
fwrite(&list[i], sizeof(struct student), 1, fp) ;
clrscr() ;
gotoxy(20, 10) ;
printf("data saved.press a key.");
getch() ;
}
//************************
void load(void)
{
FILE *fp ;
register int i ;
fp = fopen("st", "rb");
if(!fp) {
printf("\n cannot open file press a key ...");
getch() ;
return ;
}
init_list() ;
for(i = 0 ; i < SIZE ; i++) {
fread(&list[i], sizeof(struct student), 1, fp);
if(feof(fp)) {
clrscr() ;
gotoxy(20,10) ;
printf("data loaded.press a key.");
getch() ;
return ;
}//end of if
}
}
//*****************
int menu(void)
{
char s[10] ;
clrscr();
do {
gotoxy(20, 4);
printf("E) enter data ");
gotoxy(20, 6);
printf("D) display on screen");
gotoxy(20, 8);
printf("L) load file");
gotoxy(20, 10);
printf("S) save in file");
gotoxy(20, 12);
printf("Q) quit");
gotoxy(20, 14);
printf("enter your select:");
gets(s) ;
} while(!strchr("edlsq", tolower(*s)));
return tolower(*s) ;
}
---------------------------------
سوال بعدیم:xx
فرض کنید این خط کد به صورت عمومی (در بالای main) تعریف میشه....
FILE *fopen();
منظور از این خط چیه؟؟ نه به اشاره گر فایل میخوره و نه ...پس چیه....
این کتاب قمی ادما روانی میکنه....برداشته یه مثال 300 خطی نوشته ...
حتی یک دونه متغیر رو هم توضیح نداده بعد تو قسمت بعد هم میگه:
خب در قسمت قبل مسئله فایل های ترتیبی برسی شد...بر هرچی دروغ گو هست لعنت.
------البته از یه کتاب انگلیسی به اسم:
C Primer Plus, Fifth EditionBy Stephen Prata دارم میخونم ولی خب اولاش هستم....واسه توضیح یه متغیر 100 شکل کشیده و 200خط
توضیح داده--به این میگم کتاب....
ولی کتاب اون قمی رو هم شروع کردم ....نمیتونم نیمه کار رهاش کنم...
پس مجبورم مشکلاتم رو اینجا مطرح کنم ....امیدوارم در حد توان راهنماییم کنید...
ممنونم.:قلب::بوس: