My I/O Program crashes, I need some help

This is a discussion on My I/O Program crashes, I need some help within the C Programming forums, part of the General Programming Boards category; On the first run I naturally decide to Add an item. Thing is, once Iv'e added an item the program ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    My I/O Program crashes, I need some help

    On the first run I naturally decide to Add an item. Thing is, once Iv'e added an item the program immediately prompts me to update an item. When I enter 1 for updating an item I have to fill in all the details - Date borrowed, Name etc, and then the program crashes. I get something like EAcess violation under Borland C++ builder.
    Why does it prompt me for update after adding an item?

    Code:
    #define LinesPerScreen 12
    
    #include <stdio.h>
    #include <process.h>
    #include <conio.h>
    #include <string.h>
    
    char  lend_date[20];
    char  borrower_name[15];
    char  item_name[30];
    char  return_date[10];
    
    void  display_item(void);
    void  delete_item(void);
    void  add_item(void);
    void  update_item(void);
    
    void main()
    {
    
       int reply = 0;
       int items = 5;
    
    
       while (reply < items)
       {
          printf("n\n          MAIN MENU");
          printf("\n\n1. Displaying the list of lend items.");
          printf("\n2. Deleting an item from the list.");
          printf("\n3. Adding an item to the list.");
          printf("\n4. Updating an item");
          printf("\n5. (or 6, ..., etc.) EXIT the program.");
          printf("\n");
          printf("\nTo terminate choose the option 5, or higher!");
          printf("\Please choose the items to run (e.g 1, ..., 5) ");
          scanf("%d", &reply);
    
          if (reply == 1)
          {
             display_item();
          }
    
          if (reply == 2)
          {
             delete_item();
          }
    
          if (reply == 3);
          {
             add_item();
          }
    
          if (reply == 4);
          {
             update_item();
          }
    
          if (reply >= 5)
          {
             printf ("\n\nprogram terminated... ");
          }
          if (reply < 1)
          {
             printf ("\nChoice %d is out of range (retype 1, ..., 5).", reply);
          }
       }
    
       printf("\n");
       printf("\nThanks for using this program! ");
       printf("Good-bye!");
       printf("\n");
    
       system("pause");
    }
    
    void display_item()
    {
       FILE *display_file;
       char  content_in[60];
       int   item_no = 0;
       int   line = 0;
    
       system("cls");
       printf("\nThis is the list of lend items ");
       printf("\n");
    
       display_file = open ("borrowers.txt",  "r");
       fgets(content_in, 60, display_file);
    
       while(!feof(display_file))
       {
          item_no++;
          line++;
    
          if(fgets (content_in, 60, display_file))
          printf("\Item No. %d: %s", item_no, content_in);
    
          if(line == LinesPerScreen)
          {
             printf("\n");
             system("pause");
             system("cls");
             line = 0;
          }
       }
    
       printf("\n\nThis is the end of list");
       fclose(display_file);
    }
    
    void  delete_item()
    {
       FILE *origin_file;
       FILE *temp_file;
    
       char  content_in[60];
       int   item_no = 0;
       int   line = 1;
    
       system("cls");
       printf("\n           Deleting an item");
       printf("\n");
    
       if((origin_file = fopen("borrowers.txt", "r"))== NULL)
       {
          perror("Unable to open file ");
          getch();
       }
    
       temp_file = fopen("temp.txt", "w+");
    
       printf("Please enter an item number you want to delete: ");
       scanf("%d", &item_no);
    
       while(!feof(origin_file))
       {
          if(fgets(content_in, sizeof(content_in), origin_file))
             if(line != item_no+1)
                fprintf(temp_file, content_in);
             line++;
       }
    
       fclose(origin_file);
       fclose(temp_file);
    
       system("copy temp.txt borrowers.txt");
       system("del temp.txt");
    }
    
    void add_item()
    {
       FILE  *add_file;
    
       system("cls");
       printf("\n     Adding an item ");
       printf("\n");
    
       add_file = fopen ("borrows.txt", "a");
    
       printf("\nPlease enter date of book lending(in this format: 24/05/03): ");
       gets(lend_date);
    
       printf("\nPlease enter name of borrower(e.g John): ");
       gets(borrower_name);
    
       printf("\nPlease enter name of lent(e.g Networking): ");
       gets(item_name);
    
       printf("\nPlease enter date of book return(in this format: 14/06/03): ");
       gets(return_date);
    
       fprintf(add_file, "\n%s %s %s %s",
       lend_date, borrower_name, item_name, return_date);
       fclose(add_file);
    
       printf("Adding finished");
    }
    
    void update_item()
    {
       FILE *origin_file;
       FILE *temp_file;
       char  content_in[60];
       int   item_no = 0;
       int   line = 0;
    
       system("cls");
       printf("\n      Updating an item");
       printf("\n");
    
       origin_file = fopen("borrowers.txt", "r");
       temp_file = fopen("temp.txt", "w+");
    
       printf("Please enter an item you want to update: ");
       scanf("%d", &item_no);
    
       printf("\Please enter the information for this record...\n");
    
       fflush(stdin);
       printf("\nPlease enter date of book lending(in this format: 24/05/03: ");
       gets(lend_date);
    
       printf("\nPlease enter name of borrower(e.g. John): ");
       gets(borrower_name);
    
       printf("\nPlease enter name of book lent(e.g Networking): ");
       gets(item_name);
    
       printf("\Please enter date of book return(in this format: 14/06/03): ");
       gets(return_date);
    
       for (line = 1; line < item_no+1; line++)
       {
          fgets(content_in, 60, origin_file);
          fprintf(temp_file, content_in);
       }
          fprintf(temp_file, "%s %s %s %s\n",
          lend_date, borrower_name, item_name, return_date);
    
          fgets(content_in, 60, origin_file);
    
          while (!feof(origin_file))
          {
             fgets(content_in,  60, origin_file);
             fprintf(temp_file, content_in);
          }
    
          fclose(origin_file);
          fclose(temp_file);
    
          system("copy temp.txt borrowers.txt");
          system("del temp.txt");
       }

  2. #2
    Banned
    Join Date
    Aug 2004
    Posts
    128
    OK first of all
    you have a ';'

    Here :
    Code:
    if (reply == 3);
    and

    Here:
    Code:
    if (reply == 4);
    This :
    Code:
    display_file = open ("borrowers.txt",  "r");
    this should be more like :
    Code:
    display_file = fopen("borrowers.txt", "r");
    also in several a few printf f sequence you have escape characters that your not meaning to have, you should check
    those out such as \P and \I


    Fix that stuff then repost with any problems in this

    also there is a few other things in your program that ive seen
    them not recommend you use, but i dont understand some
    of it myself so i wont go into it im sure someone will post on
    it if its that big of a deal.
    Last edited by JarJarBinks; 09-23-2004 at 10:43 PM.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    71
    Okay, Iv'e removed the semicolons from the replys and I have added changed open to fopen. I also changed void main() to int main(void). But the program crashes at the line -
    Code:
    fgets(content_in, 60, display_file);
    under the line

    Code:
    display_file = fopen("borrowers.txt", "r");
    and Borland reports Function should return a value.
    Do you have any ideas?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You should check to make sure a valid file pointer is returned from fopen().

    Code:
    if(!(display_file = fopen("borrowers.txt", "r")))
    {
       printf("could not open borrowers.txt\n");
       return;
    }

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    I'm just wondering how many rules listed in the FAQ you managed to break with a single program.

    void main
    gets()
    fflush(stdin)
    while(!feof())

    Do you even pay even the remotest bit of attention to whatever anyone says on this board?

    > if (reply == 3);
    The trailing ; is a bit of a killer as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop crashes program
    By Akkernight in forum C++ Programming
    Replies: 12
    Last Post: 01-22-2009, 12:58 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. trouble with file i/o program.
    By Pyroteh in forum C++ Programming
    Replies: 7
    Last Post: 01-09-2007, 09:29 PM
  4. counting program worked, but test file CRASHES IT.. WHY?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-19-2002, 02:29 AM
  5. My program crashes with this code
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-27-2002, 11:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21