Notice: Undefined offset: 212595 in /var/www/html/qa-external/qa-external-users.php on line 744
Simple program - Problems display file in C - OnlineGDB Q&A
Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Simple program - Problems display file in C

0 votes
asked Nov 12, 2018 by (250 points)
Trying to display my file, but something goes wrong?

*********************************

//An one-card catalog
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

#define MAX 1

//function defination
  void display(int i);
  void enter(void);
  void save(void);
  void load(void);

struct catalog {
 
  char title[80];  //title
 
} cat[MAX];

int top = 0;  //last location used

//Enter title into database

void enter(void){
  int i;
  char temp[80];

  for(i = top; i < MAX; i++){

    printf("Enter title: ");
    fgets(cat[i].title, 80, stdin);

  }
  top = i;
 
}

//Display catalog entry

void display(int i){
 
  printf("%s\n", cat[i].title);

}

//Save the catalog file

void save(void){

FILE *fp;

if((fp = fopen("catalog.txt", "w"))==NULL){
  printf("Cannot open catalog file\n");
  exit(1);
 }

 fwrite(cat , 1 , sizeof(cat) , fp );

  fclose(fp);
}

//Load the catalog file

void load(void){
  FILE *fp;

  if((fp = fopen("catalog.txt", "r"))==NULL){
    printf("Catalog file ot on disk\n");
    return;
  }

  fread(cat , 1 , sizeof(cat) , fp );
 

  fclose(fp);
}

void main(void){

 printf("\nRead title, the function 1\n");

 enter();

 printf("\nSave the title on file, function 2\n");

 save();  //Save the title on file

 printf("\nLoad the title from the filen, function 3\n");

 load();  //Load the tile from file

 printf("\nDisplay the title on screen, function 4\n");

 display(top);  //Display the title on screen, function 3

}

**************************************

Please log in or register to answer this question.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...