Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Managing Data files

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 522
    Comment on it

    Hello Readers! As we all know a file is a collection of information which is stored on computer's disk and information can be saved to files and then later reused. But, what is the use of file handling why it is required?? Well, let's take an example of the e-reservation system. Although, these systems use large databases instead of file but basic concept is the same. We need a mechanism to store and retrieve data into files which can be read later on. And this mechanism is called “file handling”.

     

    File handling allows us to store data of our program on our file on the hard drives so that we can retrieve it later on.

     

    Using a file in a program is a simple three step process:

    • The file must be opened. But in case, if a file does not exists, opening means creating it.
    • Information is then saved to the file, or information is retrieved from a file, or both.
    • When a program is finished using the file, the file is closed

    The syntax for opening a file in a standard I/O package is:

    fptr = fopen(filename,mode);

    fptr is a pointer to a structure of type FILE.

     

    Mode

    Purpose

    r

    This mode specifies that the file is opened for reading only. The file must already exist.

    w

    This mode specifies that the file is opened for writing only. If the file already exist, its contents will be overwritten. Otherwise a file will be created.

    a

    This mode specifies that the file is opened only for appending a file. If the file exists, data will be added at the end of the file. Otherwise file will be created.

    r+

    This mode specifies that the file is opened for both reading and writing. The file must already exist.

    w+

    This mode specifies that the file is opened for both reading and writing. If the file already exist, its contents will be overwritten. Otherwise a file will be created.

    a+

    This mode specifies that the file is opened for both reading and writing. If the file exists, data will be added at the end of the file. If the file does not exist, it will be created.

     

    The syntax to close a file:

    flose(fptr);

    fptr => file pointer

     

    The syntax that write one character at a time:

    fputc(ch,fptr);

    ch => character variable or constant.

     

    Example:

    /* Program to demonstrate writing of one character at a time to file */

    
    main()
    
    {
    
    FILE *fptr;
    
    char ch;
    
    if((fptr = fopen(file1.dat,w)) == NULL )
    
    {
    
    printf(\n Unable to open file\n);
    
    exit(1);
    
    }
    
    printf(\n Type a line of text , when finished hit Enter key\n);
    
    while((ch=getch())!= \n)
    
    fputc(ch,fptr);
    
    fclose(fptr);
    
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: