Subject: Re: Need help with c! Approved: clc AT plethora DOT net References: From: "Marp" X-Newsreader: Microsoft Outlook Express 4.72.3110.5 Organization: Netcom X-Netcom-date: Fri Jan 22 8:01:41 AM PST 1999 Lines: 357 Newsgroups: comp.os.msdos.djgpp,comp.lang.c.moderated X-Mimeole: Produced By Microsoft MimeOLE V4.72.3110.3 Message-ID: Originator: clcm AT plethora DOT net (Comp Lang C'Moderated) Date: Sun, 24 Jan 1999 04:37:25 GMT NNTP-Posting-Host: 205.166.146.5 X-Trace: ptah.visi.com 917152645 205.166.146.5 (Sat, 23 Jan 1999 22:37:25 CDT) NNTP-Posting-Date: Sat, 23 Jan 1999 22:37:25 CDT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Get rid of the word static in front of FILE *fptr because that should be able to change as you work with the file. If it can't change it won't work properly. Mohanaraj Gopala Krishnan wrote in message ... >Hi, > >I use djgpp and am kinda new to c. I am not sure if this is the right >group to post this but I am very much hoping I can get help here. I have >this source code which has no problem when compiling and producing the >exe. But if i trace through it there is a particular function which is >problematic. The function is as follows, it a file reading function... > >#include >#include > >static FILE *fptr; >static int recsize; > >void open_file(int * pages_done, int array_size); >void close_file(int * pages_done, int array_size); >void write_file(char * strpointer, int page_size, int page_no); > >void open_file(int * pages_done, int array_size) >{ > int i=0; > > if((fptr=fopen("chapter.dat", "r")) !=NULL) { > >fseek(fptr,0L,SEEK_SET); > >for(i=0;i >fscanf(fptr,"%d",*(pages_done++)); > >} > } > else if((fptr=fopen("chapter.dat","w+b")) !=NULL) { > >fseek(fptr,0L,SEEK_SET); > >for(i=0;i >fprintf(fptr,"%d",*(pages_done++)); > >} > } > else { > printf("Cannot open or start new chapter!!!"); > exit(1); > } >} > >The problem is with the first open and read only line. If there is no >chapter.dat file in the program it is okay, but once the chapter.dat >file already exists, the program quits straight away. The function >receives two arguments, one a pointer which directs to an array of 100 >digits initialied in a different function. This array is actual;ly the >management info of the file. The rest of the file is structures. Just in >case it is hard to follow i have included all the major source code as >follows. The only thing not here is the input function which is really >just a custom input function :) Sorry if things are cluttered. > >Thank you so much. > >THIS IS THE FILE--IT CONTAINS THE MENU AND STUFF > >/* GENERAL COMMENTS:1.must customize input with esc as exit */ >/*2.must add continue+new together, asks for file, if none new, if not >continue :) */ >/*3.must add the clear screen function*/ > >#include > > >void menu(void); > >void printmenu(void); > >void FUNCT_new_page(void); > >void > >main() >{ > > > /*PAGE output_page;*/ > > menu(); > > > > /*read_page((char *)&output_page); > display_page(output_page);*/ > > return 0; >} > >void menu(void) >{ > char choice; > int done=0; > > do { > printmenu(); > choice=getch(); > > switch(choice) { > case 'n': > FUNCT_new_page(); > break; > > case 'c': > printf("This will continue the editing of >an existing chapter"); > break; > > case 'p': > printf("This will play a chapter"); > break; > > case 'q': > printf("This will quit this menu"); > done=1; > break; > > default: > break; > } > }while(!done); >} > >void printmenu(void) >{ > printf("a saradiya solutions product\n\n\n"); > printf("\t n--start new chapter\n"); > printf("\t c--continue editing existing chapter\n"); > printf("\t p--play a chapter file\n"); > printf("\t q--quit this thing"); > printf("\n\n"); > printf("\t enter yer choice "); >} > >THIS IS THE SOURCE WHICH DEALS WITH THE PAGE CHARACTERISTICS AND >FUNCTIONS > >#include > >#define STORY_LENGTH 300 >#define OPTION_1_LENGTH 50 >#define OPTION_2_LENGTH 50 >#define MAN_ARRAY_SIZE 100 > >typedef struct page { > int no; > char option_1[OPTION_1_LENGTH]; > int link_1; > char option_2[OPTION_2_LENGTH]; > int link_2; > char story[STORY_LENGTH]; > } PAGE; > > >/*will accept page number next also include the size of the structure*/ >/*void read_page(char* strpointer);*/ > >/*will return link choice next time*/ >/*void display_page(PAGE VAR_page_displaying); > >/*this function receives nothinbg it just fills up a structure and >returns it*/ >/*PAGE input_page(void); */ > >/*2nd level implementation */ >void FUNCT_new_page(void); > >/*file access stuff in file.c*/ >void open_file(int * pages_done, int array_size); >void close_file(int * pages_done, int array_size); >void write_file(char * strpointer, int page_size, int page_no); > >/*page stuffin here */ >void display_page(PAGE VAR_page_displaying); >PAGE input_page(void); > >void read_page(char * strpointer) >{ > FILE *page_write; > PAGE page; > > page_write=fopen("chapter.dat","r"); > if(page_write==NULL) > { > puts("No chapter found!!"); > return; > } > > fread(strpointer,sizeof(page),1,page_write); >} > >void display_page(PAGE VAR_page_displaying) >{ > printf("Page number: %d\n\n",VAR_page_displaying.no); > printf("Text: %s\n\n",VAR_page_displaying.story); > printf("%s press 1\n",VAR_page_displaying.option_1); > printf("%s press 2\n",VAR_page_displaying.option_2); >} > >PAGE input_page(void) > >{ > PAGE page; > > printf("\n"); > printf("Page no please:"); > scanf("%d",&page.no); > > printf("\n"); > printf("Enter text for story\n"); > input(page.story, STORY_LENGTH); > > printf("\n"); > printf("Enter text for 1st Option:"); > input(page.option_1, OPTION_1_LENGTH); > printf(" Enter page link for 1st Option:"); > scanf("%d", &page.link_1); > > printf("\n"); > printf("Enter text for 2nd Option:"); > input(page.option_2,OPTION_2_LENGTH); > printf(" Enter page link for 2nd Option"); > scanf("%d", &page.link_2); > > return(page); >} >/*must add a function to detect if page has been filled */ >void FUNCT_new_page(void) >{ > PAGE VAR_input_page; > int done=0; > char choice; > int pages_done[MAN_ARRAY_SIZE]; > int i=0; > > for(i=0;i pages_done[i]=0; > } > > i=0; > > open_file(pages_done,MAN_ARRAY_SIZE); >; >/* a do while loop goes in here to allow many pages, page is created , >shown, ask if want to be saved and then ask if want new page or quit*/ > do { > > > VAR_input_page=input_page(); > > pages_done[i++]=VAR_input_page.no; > > > printf("\n\n This is the page you just created:\n"); > display_page(VAR_input_page); > > printf("\n\n"); > > printf("Do you want to save it?(y/n)\n"); > choice=getch(); > if(choice=='y') { > write_file((char *)&VAR_input_page, >sizeof(VAR_input_page),VAR_input_page.no); > } > > printf("Do you want to continue adding pages(y/n)"); > choice=getch(); > if(choice=='n') { > done=1; > } > }while(!done); > > close_file(pages_done,MAN_ARRAY_SIZE); >} > > >THIS SOURCE DEALS WITH THE DISK WRITE FUNCTIONS > >#include >#include > >static FILE *fptr; >static int recsize; > >void open_file(int * pages_done, int array_size); >void close_file(int * pages_done, int array_size); >void write_file(char * strpointer, int page_size, int page_no); > >void open_file(int * pages_done, int array_size) >{ > int i=0; > > if((fptr=fopen("chapter.dat", "r")) !=NULL) { > >fseek(fptr,0L,SEEK_SET); > >for(i=0;i >fscanf(fptr,"%d",*(pages_done++)); > >} > } > else if((fptr=fopen("chapter.dat","w+b")) !=NULL) { > >fseek(fptr,0L,SEEK_SET); > >for(i=0;i >fprintf(fptr,"%d",*(pages_done++)); > >} > } > else { > printf("Cannot open or start new chapter!!!"); > exit(1); > } >} > > > > >void close_file(int * pages_done, int array_size) >{ > int i=0; > > fseek(fptr,0L,SEEK_SET); > for(i=0;i fprintf(fptr,"%d",pages_done[i]); > } > fclose(fptr); >} > >void write_file(char * strpointer, int page_size, int page_no) >{ > fseek(fptr,(page_no)*page_size ,SEEK_SET); > fwrite(strpointer, page_size, 1, fptr); >} > > > > > > >-- >comp.lang.c.moderated - clcm AT plethora DOT net -- comp.lang.c.moderated - clcm AT plethora DOT net