www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/17/14:00:46

Subject: Need help with c!
Approved: clc AT plethora DOT net
From: Mohanaraj Gopala Krishnan <saradiya AT bigfoot DOT com>
Organization: Unconfigured
X-Accept-language: en
Lines: 349
Mime-Version: 1.0
Newsgroups: comp.os.msdos.djgpp,comp.lang.c.moderated
Message-ID: <clcm-19990117-0010@plethora.net>
X-Mailer: Mozilla 4.5 [en] (Win98; I)
Originator: clcm AT plethora DOT net (comp.lang.c.moderated)
Date: Sun, 17 Jan 1999 18:55:49 GMT
NNTP-Posting-Host: 205.166.146.1
X-Trace: ptah.visi.com 916599349 205.166.146.1 (Sun, 17 Jan 1999 12:55:49 CDT)
NNTP-Posting-Date: Sun, 17 Jan 1999 12:55:49 CDT
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 <stdio.h>
#include <stdlib.h>

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<array_size;i++) {

fscanf(fptr,"%d",*(pages_done++));

}
                                                      }
        else if((fptr=fopen("chapter.dat","w+b")) !=NULL) {

fseek(fptr,0L,SEEK_SET);

for(i=0;i<array_size;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 <stdio.h>


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 <stdio.h>

#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<MAN_ARRAY_SIZE;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 <stdio.h>
#include <stdlib.h>

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<array_size;i++) {

fscanf(fptr,"%d",*(pages_done++));

}
                                                      }
        else if((fptr=fopen("chapter.dat","w+b")) !=NULL) {

fseek(fptr,0L,SEEK_SET);

for(i=0;i<array_size;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<array_size;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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019