#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    1
    Rep Power
    0

    Appending to file


    Hi all, I have this problem in my C program.. I tried several ways to fix it buy couldn't.. any help would be greatly appreciated.

    I have this method it writes student information and saves them in a file.

    void Writing()
    {
    int student_number=0;
    char name[100];
    int nicn=0;
    char gender[100];
    char dob[100];
    int contact=0;
    char nationality[100];


    printf("Welcome to the registring system\n\n");
    printf("Please enter student number: ");
    scanf("%d",&student_number);


    printf("Enter student name: ");
    scanf("%s",&name);


    printf("Enter national identification card number: ");
    scanf("%d",&nicn);

    printf("Input gender: ");
    scanf("%s",&gender);


    printf("Enter date of birth: ");
    scanf("%s",&dob);

    printf("Enter contact number: ");
    scanf("%d",&contact);

    printf("Enter Nationality: ");
    scanf("%s",&nationality);


    FILE *file;
    file = fopen(name,"w+"); /* apend file (add text to
    a file or create a file if it does not exist.*/
    fprintf(file,"ID number: %d\n",student_number );
    fprintf(file,"Name: %s\n",name);
    fprintf(file,"NICN: %d\n",nicn);
    fprintf(file,"Gender: %s\n",gender);
    fprintf(file,"DOB: %s\n",dob);
    fprintf(file,"Contact Number: %d \n",contact);
    fprintf(file,"Natunality: %s\n",nationality);
    fclose(file); /*done!*/

    printf("Student successfully added!");
    }

    Then I made another method which is to read the file and append to it.. but the problem is when I try to append, it gives me a run time error.. here is the method:

    void Appending()
    {
    char name1[100];

    printf("Enter file you want to append to: ");
    scanf("%s", &name1);

    int student_number=0;
    char name[100];
    int nicn=0;
    char gender[100];
    char dob[100];
    int contact=0;
    char nationality[100];


    printf("Welcome to the Edit section!\n\n");
    printf("Please enter student number: ");
    scanf("%d",&student_number);

    printf("Enter national identification card number: \n ");
    scanf("%d",&nicn);

    printf("Input gender :\n");
    scanf("%s",&gender);


    printf("Enter date of birth: \n");
    scanf("%s",&dob);

    printf("Enter contact number:\n");
    scanf("%d",&contact);

    printf("Enter Nationality: \n");
    scanf("%s",&nationality);


    FILE *file;
    file = fopen(name,"a+");
    fprintf(file,"ID number: %d\n",student_number );
    fprintf(file,"Name: %s\n",name);
    fprintf(file,"NICN: %d\n",nicn);
    fprintf(file,"Gender: %s\n",gender);
    fprintf(file,"DOB: %s\n",dob);
    fprintf(file,"Contact Number: %d \n",contact);
    fprintf(file,"Natunality: %s\n",nationality);
    fclose(file); /*done!*/
    }

    Thanks!
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    159
    Rep Power
    19
    Part of your problem lies with your scanf() calls. When you try to retrieve a C-string you don't need the ampersand, the array name is all that is required. This a problem in both of your functions.

    Jim
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Location
    Iran
    Posts
    149
    Rep Power
    140
    The code is not really structured and there are redundant elements.

    I would define a student as a structure, so that the information can be held and defined in a more compact form.

    Also, if you take a look at the functions Writing() and Appending(), both are almost the same except that they differ in how the file is being opened, that is for a first time writing (file creation) or for an append.


    Regards,
    Dariyoosh

IMN logo majestic logo threadwatch logo seochat tools logo