Thread: file problem

  1. #1
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41

    file problem

    HII!
    i have to create a file "family.TXT" whose the informations are structured by this way:
    Family name
    Father's name
    Mother's name
    Number of children
    Names of children

    i attempt this code it works until i type "Number of children" it display an error message
    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <string.h>
    typedef struct
        {
        char n_family[20];
        char n_father [20];
        char n_mother [20];
        int nbr_enf;
        char n_child[20];
        }family;
        family nbr[20];
    int main(int argc, char *argv[])
    {
      int k=0;
    FILE * fichier=NULL;
    fichier=fopen("w","family.TXT");
     if (fichier != NULL)
        {
          printf("there is a problem \n");
            fclose(fichier);
        }
        printf("family name :");
        printf("\n");
        scanf(" %s",&nbr[k].n_family);
        printf("father's name :");
        printf("\n");
        scanf(" %s",&nbr[k].n_father);
        printf("mother's name:");
        printf("\n");
        scanf(" %s",&nbr[k].n_mother);
        printf("Number of children");
        printf("\n");
        scanf(" %d",&nbr[k].nbr_enf);
        fprintf(fichier,"%s\n%s\n%s\n%d\n",nbr[k].n_family,nbr[k].n_father,nbr[k].n_mother,nbr[k].nbr_enf);
        fclose(fichier);

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What error message?

    Also for %s format you should remove & since array name is converted to pointer automatically

    Also note - that if you succeed in opening the file - you close it and then try to output into the closed handle

    If you fail to open the file - you still output into the NULL pointer

    Both operations should fail
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41
    it's a memory message error "0x7c91100b"

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You probably shouldn't be mixing English and French in your code. Best to stick with English if you're asking for help from a bunch of English speakers! (I'm thinking of your use of "fichier" instead "file" and "enf", presumably meaning "enfants", instead of "children".)


    And what does "nbr" mean? I thought "number", but it doesn't make sense since you call your entire family array nbr. And why are all the family struct members prefixed with n_ ? What does that mean?


    Now the logic. You'll should exit the program if the file doesn't open (and you shouldn't attempt to close it).


    There's no reason to print the \n character with a separate printf.


    You shouldn't put the ampersand & in front of string members. They are already pointers, so the & is incorrect there. That's the problem you're having.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User slyer4ever's Avatar
    Join Date
    Oct 2013
    Posts
    41
    yes i'm sorry for the confusion
    I have solved the problem thank you guys

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    1
    there is no problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File handing and saving records to file problem
    By loveable in forum C Programming
    Replies: 18
    Last Post: 12-29-2012, 09:53 AM
  2. Problem with Creating File for File Processing
    By Dampecram in forum C Programming
    Replies: 2
    Last Post: 12-07-2008, 01:26 AM
  3. Replies: 3
    Last Post: 11-21-2006, 07:26 PM
  4. Replies: 3
    Last Post: 10-20-2006, 07:59 PM
  5. Replies: 3
    Last Post: 05-03-2003, 12:04 PM