Thread: Program compiled and then crash

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    1

    Program compiled and then crash

    Code:
    #include int main(){    char surname[10];    int name;    int matricno;    int mit801, mit802, total;    FILE *file, *file1;    /* Open an existing file */   file1 = fopen("newbroadsheet.txt", "w");   file = fopen("Broadsheett.txt", "r");    if ((file = fopen("Broadsheett.txt","r")) == NULL){        perror("Error: Unable to open a file");      }        else {        fprintf(file1, "University of Lagos\n");        fprintf(file1, "================================\n");        fprintf(file1, "Faculty of Sciences\n");        fprintf(file1, "================================\n");        }    do  {        fscanf(file, "%s%d%d%d", name, &matricno, &mit801, &mit802);        fprintf(file1, "%s\t%d\t%d\t%d", name, matricno, mit801, mit802);    } while (!feof(file));    fclose(file);    fclose(file1);    return 0;}

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Perhaps you can try again, and this time review your post to make sure it's readable before pressing submit.
    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.

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    It's a small code, so I have correct the format for a better visibility in the forum.
    Code:
    #include 
    
    int main(){
    
        char surname[10];
        int name;
        int matricno;
        int mit801, mit802, total;
        FILE *file, *file1;
    
        /* Open an existing file */
        file1 = fopen("newbroadsheet.txt", "w");
        file = fopen("Broadsheett.txt", "r");
    
        if ((file = fopen("Broadsheett.txt","r")) == NULL){
            perror("Error: Unable to open a file");
        }
        else {
            fprintf(file1, "University of Lagos\n");
            fprintf(file1, "================================\n");
            fprintf(file1, "Faculty of Sciences\n");
            fprintf(file1, "================================\n");
        }
    
        do  {
            fscanf(file, "%s%d%d%d", name, &matricno, &mit801, &mit802);
            fprintf(file1, "%s\t%d\t%d\t%d", name, matricno, mit801, mit802);
        } while (!feof(file));
    
        fclose(file);
        fclose(file1);
        return 0;
    }
    Line 1: i think that should be
    Code:
    #include <stdio.h>
    You open the file "Broadsheett.txt" two times (line 13 and 15).
    And I think you have confused your self with the variables 'name' and 'surname'.
    'name' is of type 'int' and 'surname' is a array of type 'char'.
    Now look at your 'fscanf' and 'fprintf' functions.

    And you should check if the file was opened successfully (both file).
    I would write it this way:
    Code:
    …
        if ((file = fopen("Broadsheett.txt", "r")) == NULL) {
            perror("Error: Unable to open the file to read! exit.\n");
            return 1;
        }
    
        if ((file1 = fopen("newbroadsheet.txt", "w")) == NULL) {
            perror("Error: Unable to open the file to write! exit.\n");
            fclose(file);
            return 2;
        }
    …
    Other have classes, we are class

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. running compiled program in Emacs
    By JonnyFrond in forum Linux Programming
    Replies: 4
    Last Post: 12-22-2011, 02:29 PM
  2. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  3. Memory Representation of compiled C Program
    By gjagadish in forum C Programming
    Replies: 2
    Last Post: 04-03-2008, 03:02 AM
  4. Replies: 1
    Last Post: 03-12-2008, 12:10 AM
  5. help with compiled program
    By wraith8 in forum C Programming
    Replies: 14
    Last Post: 08-07-2003, 07:07 AM

Tags for this Thread