Data Files in C

Data Files in C

Concept of data files

We use scanf()function to take input from users through the keyboard, similarly printf()for display data on the computer screen and so many built-in functions. In such a program, data is temporarily stored in the main memory during the program execution. When the program is re-complied, the entered data have been lost. Therefore, we need data files to permanently stored data. A data file is simply a file on the hard disk, which is used to store data permanent data file in C

A data file is defined as the collection of data or information, which is permanently stored inside secondary memory as a single unit. We can read, write append and delete data in the data files as per our requirement.

 Data file function in C program 

C language supports the number of functions to perform various operations such as defining file, opening file, reading a file, writing a file, appending file, and closing file. The sum function is shown below

Function

Use

fopen()           

To create the new data file

fclose()

To close data files

getc()             

To read a character from data files

putc()             

To write a character from data files

getw()            

To read an integer from the data file

putw()            

To write an integer to the data file.

fscanf()           

To read formatted data to data files

fprintf()           

To write formatted data to data files    

fread()            

To read record from data files

fwrite()           

To write a record to data files

 Description of file function

Opening/closing 

        I.        fopen()

fopen() is used to open the file. Before a program can write to a file or read from a file, the program must be open.

Syntax

fclose(ptr _ variable);

 

     II.               fclose

fclose() is used to close the file. After opening a file a program must be close.

Syntax

fclose(ptr _ variable);

 Formatted Input / output

fprintf()

fprintf() is used to write some integer, float, char, or string to a file. It is a file version of printf()

Syntax

fprintf(“ file _ ptr _ variable ”, “control – string”, list - variable);

 

Example

  fprintf(f, “ Name = %s”, s);

Here

fis a flie name declare under FILE *f;

Name = %s is a control string

s is a list variable declare under char s;

fscanf()

fscanf() is used to read some integer, float, character or string from a file. It is a file version of scanf()

fscanf(“ file – ptr_variable”, “control string”, &list _ variable);

 

Example

fscanf(f, “%d”, &num);

Here

File name declare under FILE *f;

%d is a control string

&num is a list _ variable declare under int num;

 Types of file

Text file: - The file there is a provision of .txt is called text files. This file can be created using a simple text editor.

Binary file:- .bin are binary file. Some people use .bin instead of .txt. Because it can hold a large amount of data, it is not easy to read and it considers high security than .text file

Defining and Opening Data files

To opening the file fopen() is used which is define under header file #include<stdio.h>

 

The following syntax is used to define data files

FILE * variable;

variable = fopen(“file name”, “mode”);

Example

FILE *fp;

fp=fopen(“Record.tex”, “w”);

 

FILE *b;

b = fopen(“Binary.bin”, “wb”);

 

 

The keyword FILE is used to declare file data type. The first statement defines the file pointer variable with data type FILE. The second statement creates a file with the name filename and returns the memory address that is assigned to the file pointer variable. The mode defines the purpose of the file, which can be one of the followings:

File opening mode in C

Mode

Meaning of mode

r

open an existing file for reading

rb

open an existing file for reading in binary mode

w       

open a new file for writing

wb

open a new file for writing in binary mode

a

open an existing file for appending

ab

open an existing file for appending in binary mode

r+

open an existing file for both reading and writing

r+b

open an existing file for both reading and writing in binary mode

w+

open a new file for both reading and writing

w+b

open a new file for both reading and writing in binary mode.

a+

open an existing file for both reading and appending

a+b

open an existing file for both reading and appending in binary mode

 

The compiler automatically assigns a special character at the end of the file called EOF end of the file.

Closing Data Files

To closing the file fclose() function use. Which is also define in header file #include<stdio.h>



A data file must be closed after doing various operations such as reading, writing, etc. It ensures that all the information associated with the file is flushed out from the memory and all links to the file are broken. It also helps us to protects from accidental loss of data from the file, The following syntax is used to close the file.

 The following syntax is used to close the  files

fclose(file pointer);

Examples

fclose(fp);

fclose(b);

 


General guidelines for solving a file related problems

Step 1:Define the file pointer variable with type FILE either inside or outside the main() function.

FILE *fp;

Step 2:For the first time creates a new data file using fopen function in write mode “w”.

fp = fopen(“file name”, “w”);

Step 3: Take data from the keyboard using input functions: scanf(), gets(), and getchar()and store the data to the file using write functions: fprintf(), putc(), putw()and fwrite().

Step 4:During the read operation, first open the data file in read mode.

fp = fopen(”fileName”, “r”);

Step 5:Read the data from the file using read functions: fscanf(), getc(), getw()and fread()and display the data on screen using output functions : printf(), puts() and putchar().

Step 6: Finally close the data file using function fclose().

flose(fp);

Summary of the above paragraph

 Before a program can write to a file or read from a file, the program must open it. Opening a file establishes a link between a program and the OS. Buffer area needs to be established as FILE *ptr _ variable.

The FILE *ptr _ variable is a pointer to the datatype FILE that stores the beginning address of the buffer area allocated after the file has been opened. A data file is open using ptr _ variable = fopen(file _ name, “ mode “);

After opening a file, we can process data files as required. Finally, the data file must be closed. fclose(ptr _ variable)

Reading writing to a file in .text

To read a file we use fprintf() function which is a file version of function printf() and To write a file we use fscanf()which is also a file version of function scanf().

Example:- Write to a file in .text

#include<stdio.h>

#include<conio.h>

int main()

{

          FILE *fp;

          char name[20];

          fp = fopen("Name.txt", "w");

          printf("\n Enter a name");

          scanf("%s", name);

          fprintf(fp, "Name=%s", name);

           fclose(fp);

           getch();

}

The function takes Name as an input from the user and stores it in Name.txt

If you compile and run the program, After a run and compile you can see file Name.txt you can see from where you save the program on your computer. When you open the file you can see the name you entered

Example:- Read to a file in .text

#include<stdio.h>

#include<conio.h>

void main()

{

          FILE *fp;

          char name;

          fp = fopen("Name.txt", "r");

          fscanf(fp, "%s", &name);

          printf("\n Name = %s", name);

          fclose(fp);

          getch();

}


Post a Comment

0 Comments