Thread: File handling help

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    7

    Exclamation File handling help

    Hi all

    I am trying to make a program that will ask you to enter your name, occupation and age of 3 people and then save them to a file. I can get it to allow me to enter the first persons name age and occupation and then the second one messes up on me. Also when i go to the saved file my first persons details are missing.
    Anyones help on how to modifiy my program is greatly appriciated.
    steve

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    #include <stdio.h>
    #include <conio.h>
    int main()
    {
        FILE * newfile;
        char name[30];
        int age;
        char occupation[30];
        int i;
        
        for (i=0; i<3; i++)
        {
        printf("\nEnter your name: ");          /* enter name        */
        gets(name);                              /* gets name       */
        printf("\nEnter your occupation: ");     /* enter Ocupation */
        scanf("%s", occupation);                 /* gets occupation */
        printf("\nEnter your age: ");            /* enter age       */
        scanf("%i",&age);
    }
        
        newfile = fopen("employee.dat","a");   /* open the file*/
        
        fprintf(newfile,"%s\n",name);             /* saves name in file */
        fprintf(newfile,"%i\n",age);              /* saves age in file */
        fprintf(newfile,"%s\n",occupation);       /* saves Occupation in file */
    
        
        fclose(newfile);
        return(0);
    }
    A couple of problems
    1) never use gets(), see the FAQ.
    2) never mix a 'get' string function (ie, fgets()), with scanf. scanf typically leaves newlines, which is exactly what fgets looks for.
    3) your arrays are not 3 elements long.

    Code:
        char buff[100];
        FILE * newfile;
        char name[3][30];
        int age[3];
        char occupation[3][30];
        int i;
        
        for (i=0; i<3; i++)
        {
            printf("\nEnter your name: ");
            fgets( buff, sizeof buff, stdin );
            sscanf( buff, "%s", name[i] );
    
            printf("\nEnter your occupation: ");
            fgets( buff, sizeof buff, stdin );
            sscanf(buff, "%s", occupation[i] );
    
            printf("\nEnter your age: ");
            fgets( buff, sizeof buff, stdin );
            sscanf(buff, "%i", &age[i] );
        }
    Using fgets() for everything allows you to separate input from conversion. Both fgets() and sscanf() return a status which should be checked.

    Ideally, the sscanf for the strings should limit the length of the input which can be copied.


    Use the same kind of loop to print each array element to the file, say
    fprintf(newfile,"%s\n",name[ i ] ); /* saves name in file */
    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
    Apr 2007
    Posts
    7
    Thanks for the reply on that salem,

    Can i ask a few questions that i am unsure of, what is the purpose of

    fgets( buff, sizeof buff, stdin ); i understand what the buff and sizeof buff are doing but what is the stdin mean or do?

    Also i have got my program to work as i would like, but when you enter for example

    name: joe bloggs
    age: 21
    occupation: computer techinican

    in the saved file only
    joe
    21
    computer

    will be displayed. Is that because i am using the sscanf function? and when it sees a space it thinks the name is finished? How could i fix this?

    Regards
    Stephen

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    stdin is the standard input stream, commonly mapped to your keyboard. It's what you read input from by default.

    &#37;s stops at whitespace. You can get around this by using some other format specifiers, such as %[^\n] to make it read anything up to the newline, excluding the newline.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    Quote Originally Posted by quzah View Post
    stdin is the standard input stream, commonly mapped to your keyboard. It's what you read input from by default.

    %s stops at whitespace. You can get around this by using some other format specifiers, such as %[^\n] to make it read anything up to the newline, excluding the newline.


    Quzah.
    Thnak you for your response, im quite new to this programming and still not very good at it. I have attached my program, could you please tell me what i should be putting in instead of the %s to make it read the likes of the full name.

    Many thanks
    Stephen

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. I'm not sure anyone here can. Sorry.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    What you mean? Am i doing it complety the wrong way or what? I can get it to print the first name just will not put the next name in.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Quzah already gave you the answer, replace the "&#37;s" in the first sscanf call with "%[^\n]"

    Then read about scansets (and the complement scan sets which a leading ^ specifies) in your book / tutorial / lecture notes.

    Oh, and can you paste short code such as this directly into the post rather than attaching it, and use [code][/code] tags.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM