SlideShare ist ein Scribd-Unternehmen logo
1 von 6
UNIT 8
                                                           FILES
File: A file is a place on the disk where a group of related data is stored. It is a permanent named unit that holds
large volume of related data. It stores the data in secondary or auxiliary memory such as CDs, tapes and Hard disk.
Hence, it is permanently stored memory. It is used to support the volatile nature of main memory. That is, when the
system is shutdown, main memory looses the data and relies on secondary storage. When the system is switched on,
main memory gets loaded with the data from the files. In addition to reading data from files, main memory will also
write data into the files if it has made any changes to the data.
             In addition to reading data from files, main memory will also write data into the files if it has made any
changes to the data. A buffer is such a temporary storage used for data transfers between main memory and
secondary memory.
File name: The file name is typically a string of characters. The file names can be “Data.txt”, ”mul.txt”, ”prg.c” or
“Div.n” etc.
       A file name may contain two parts, a primary name and optional period with the extension.
Ex: input.data, student.c etc
File Information Table: In order to read or write a file in a program, certain information is required like file name,
the location of the current character of the file etc. such information is stored in a predefined structure called file
information table. This table is defined by “stdio.h” header file with “FILE” as its identifier. Hence, whenever a file
is to be read or written, it is declared using the „FILE‟ type.
Stream: “A stream is a flow of data bytes between a program and files.” A stream can be either a source of data (in
case of a write operation).
       The write-end of a stream is called an input stream. The read-end of a stream is called an output stream.
        „C‟ language provides stream as the basic for all types of operations on files. The files generally are stored on
some secondary storage. Such as disks, tapes etc. When a „C‟ program has to operate on files it uses standard library
functions declared in header file stdio.h. All those functions either read or write data from or to the file through
streams i.e., All input output related functions use streams as the communication.

Redirecting I/O to a File :
       Both read and write file operations can be easily performed under many operating systems. If we want to write
program results into a file, then command.

       prog > data, will execute the program with the name „prog‟ and writes the output to the file called data.

       We can redirect input from file also
       Ex : reverse <file1

       This command takes the value from file „file1‟ and redirects to program called „reverse‟.

       We can redirect input and output to the program at the same time.
       Ex : reverse < number > data

       This command reads all the input for the program „reverse‟ from file „number‟ and write all the output to the file
„data‟.
End of file :
        And end-file condition exists when the final piece of data has been read from a file. Attempting to read after end
of file might cause the program to terminate with an error (or) it might cause the program to go into an infinite loop.
EOF() function is defined in the standard I/O include file <stdio.h>

                                      Special functions for working with Files
Following are the main steps involved file processing
                        1. Opening File
                        2. Processing a File
                        3. Closing a File
Defining file Pointer :

        A file is opened, accessed, closed through a file pointer. A file pointer is declared using „FILE‟, which is defined
in <stdio.h>.

       FILE *name of the file pointer;
       Ex : FILE * fp;

The fopen () function :

         Before doing any I/O operation on a file, the file must be opened. To open a file, we must specify the name of
the file. The function fopen() is used to open (or) create a file. The general format of the fopen() function is

       fopen (“filename”, “mode);

       The function fopen () takes two arguments
       i) filename ii) system type operation i.e. mode

Modes :

       A file may be opened in one of the following modes

„w‟ mode               This mode opens the file in write mode. Only file write is possible
„r‟ mode               This mode opens the file in read mode. Only file read is possible
„a‟ mode               This mode opens the file in append mode. In this mode data is added
                       from the end of the existing data.
„w+‟ mode              Write mode with read option
„r+‟ mode              read mode with write option
„a+‟ mode              append mode with write option

      If we add „b‟ i.e „wb‟, „rb‟ etc., the file will be opened in binary mode for corresponding system operation.
      Ex : fp =fopen(“student.dat”, “w”);
      This statement opens a file “student.dat” in write mode and return the file address into file pointer „fp‟.
      The fopen() function opens the file, but if file is not existing the function creates a file.
The getc() and putc() Functions :

getc() : The getc() function inputs one character from a specified file stream. The syntax of getc() is :

       char getc (FILE * stream);
       Ex : char flag ;
           flag = get c (fp);
       This statement inputs a character from the file defined by fp.

putc() : The putc() function outputs one character to the file stream represented by the specified file pointer. The
syntax of putc() is :
        char putc (char ch, FILE * stream);
        Ex : putc (flag, fp);
        This statement writes the character „flag‟ to file pointed by „fp‟
The fclose () Function :
        When operations are completed on a file, it is good habit to close the file. When a program terminates normally,
the system automatically closes any open files. It is generally better programming practice to close a file, as soon as we
are done with it. fclose() function is used to close an opened file.

       The general format of fclose() is :
fclose (filepointer);

       The argument to the fclose () is filepointer.
       Ex : fclose (fp);
       Closes the file defined by the file pointer „fp‟.

The feof () Function :
       The feof() function checks the file position indicator to determine if the end of the file associated with stream has
been reached. A nonzero value is returned for the end of the file, otherwise zero is returned.

        Syntax : feof (filepointer);
        Ex : while (! feof(fp))
                {
                 .....
                 .....
                }
        The loop continues till end of the file.
Ex : Following program explains copying contents of one file to another file :
        #include <stdio.h>
        main ()
        {
          FILE *fp1, *fp2;
          char flag;
          fp1 = fopen (“Charadata1.data”, “r”);
          fp2 = fopen (“Charadata2.data”, “w”);
          while (! feof(fp))
          {
            flag = getc (fp1);
            putc (flag, fp2);
          }
          fclose (fp1);
          fclose (fp2);
          getch();}
        In order to run above program the file chardata1.data should exists i.e. it should be created. The while loop
copies the contents of file chardata1.data to chardata2.data.

The fprintf () Function :
       The fprintf () is like printf ( ) except that this function writes the data to a specified file. This function takes an
additional parameter, which is the FILE pointer that identifies the file to which data is to be written.

       Ex : fprintf (fp, “programming in c is fun n”);
       This statement writes the specified string into a file identified by the file pointer fp.

The fscanf () Function :
       This function is used to read data from a file. The general format is
       fscanf (File pointer, “control characters”, & variablenames);

       Ex : fscanf (fp, “%d %f”, &a, &b);

        This statement reads an integer and float value assigns them to variables „a‟ and „b‟ respectively from a file
identified by „fp‟.

The fgets () Function :
fgets() function is used to read a string from a file. The syntax of fgets() is as shown below:

       char *fgets(char *str, int num, FILE * stream);
       The fgets() function reads upto „num‟ characters from a file pointed by „stream‟ and places them into the
character array pointed to by „str‟. Characters are read until a new line or until the specified limit is reached.

Ex:    fgets(str, 20, fp);

      This statement reads a string of length 20 pointed by filepointer „fp‟ to the character array „str‟.
The fputs () Function :
      fputs() function is used to write a string on to the file. The syntax of the fputs() frunction is :

       fputs(char *str, FILE *stream);

fputs() function writes the contents of the string pointed to by „str‟ to the specified stream. The null terminator is not
written.
Ex: fputs(str, fp);

       This statement writes the string „str‟ in to the file pointed by the file pointer fp.

Ex : Programs to perform file operations using fgets() and fputs :

# include “stdio.h”
main ()
{
  FILE *fp;
  int n;
  char str[5];
  printf(“n Enter No.of Strings :”);
  scanf(“%d”, &n);
  fp =fopen(“Names.dat”, “w”);
  for (i=1; i<=n; i++)
  {
    scanf (“%s”, str);
   fputs (str, fp);
  }
 fclose (fp);}
The above program creates a file “names.dat” and writes „n‟ strings using fputs.
main()
{
 FILE *fp;
 char str[5];
 fp = fopen (“names.dat”, “r”);
 while (!feof(fp))
  {
    fgets(str, 5, fp);
    prints(“n%s”, str);
   }
fclose (fp);}
         This program opens the file names.dat in read mode. The while loop reads string by string and display them on
the screen, until end of the file.
stdin, stdout, stderr :

       In „C‟ when the program begins execution, the following three files will be automatically opened.
1. Standard input, stdin
        2. Standard output, stdout
        3. Standard error, stderr
        By default, the stdin, stdout, stderr refer to user‟s console. This means that whenever a program expects input
from the standard input, it receives that input from stdin. A program that writes to the standard output prints it data to
stdout. Any error messages that are generated by the library routines are sent to stderr.

fwrite() : This function is used to write a record (or) structure to the file.
               fwrite(&s, sizeof(structure),1,fp);

        Structure „s‟ will be stored into the file pointed by fp.

fread() : This function is used to read a record / structure from the file.
       fread(&s, sizeof(structure),1,fp);

        Structure „s‟ will be read from the file pointed by fp.

ftell () : This function is used to locate the position of the file pointer. It returns a long
          integer.
          Ex : Long int L;
                L=ftell (fp);

fseek() : This function is used to set the file pointer to the desired position.
        fseek( fp, ibytes, pos);

rewind() : This function sets the file pointer to the initial position of the file.
              Ex : rewind(fp).

The exit() Function :

       To explicitly terminate a program, the function exit () will be called.
       exit (n) ; has the effect of terminating the current program. Any open files are automatically closed by the
system. The integer „n „ is called exit status.
Renaming and Removing Files :
        rename () function is used to rename the existing file.
        Ex : rename (“tempfile”, “database”);
        This statement renames the file “tempfile” to “database”.
        remove () function deletes the file specified by its argument.
        Ex : remove (“tempfile”);
This statement deletes the file “tempfile

Random Accessing Files :
                  Generally a file is accessed sequentially. For random access data „C‟ supports different functions.
ftell () : This function is used to locate the position of the file pointer. It returns a long integer.
          Ex : Long int L;
                 L=ftell (fp);
fseek() : This function is used to set the file pointer to the desired position.
          fseek( fp, ibytes, pos);
rewind() : This function sets the file pointer to the initial position of the file.
          Ex : rewind(fp).
To access the record randomly :
                           i. Read the present file pointer position using ftell().
ii. Set the file pointer to initial position using rewind().
                        iii. Set the file pointer to required position by using fseek().
Text files and Binary files:
             The text files are those files that contain letters, digits and symbols. A text file mainly contains characters
coded from the ASCII character set. On the other hand, a binary file is a file that uses all eight bits of bytes for
storing information. The text file is mainly in a form, which can read and understood by human beings. The binary
file, on the other hand, is generally in a form, which can be interpreted and understood by a computer system.

          In contract to a binary file, the text file uses only seven bits allowing the eighth bit to be zero. In a
computer system, all executable or .exe files. Dynamic Link Library(DLL) and many database files are stored as
binary files. One of the differences between text and binary file is that the data contained in the text file can be read
by a word processor while the binary file data cannot be read by a word processor. In „C‟ when you access a file for
reading and writing then you can choose to open the file as binary or text file. „C‟ programming language provides
the fopen() function to open a file in a specific mode.

         When we specify the mode with the fopen() function, you can choose to open the file in a read mode for
reading, write mode for appending data to the end of a file. It is also determines at the time of opening a file using
fopen() function that whether the file should be opened as a binary or text file. The „C‟ programming language
provides different read, write and append modes for reading and writing to text and binary files. If you use the
following modes with fopen() function then the file is opened as a text file.

       r: this mode allows you to open a file as a text file for reading data from it.
       w: this mode allows you to open a file as a text file for writing data to it.
       a: this mode allows you to open a file as a text file for appending data at the end of the file.
       r+: this mode allows you to open a file as a text file for reading data as well as writing data to a file.
       w+: this mode allows you to open a file as a text file for writing data as well as reading data from a file.
       a+: this mode allows you to open a file as text file for both reading and writing data to a file.

      We should add „b‟ for binary file with the r,w and a modes to a file as a binary file, you can use the following
   modes:

       rb: this mode allows you to open a file as a binary file for reading data from it.
       wb: this mode allows you to open a file as a binary file for writing data to it.
       ab:this mode allows you to open a file as a binary file for appending data at the end of the file.
       rb+: this mode allows you to open a file as a binary file for reading as well as writing data to a file.
       wb+: this mode allows you to open a file as a binary file for writing data as well as reading data from a file.
       ab+: this mode allows you to open a file as binary file for both reading and writing data to a file.

      In „C‟, the process for writing of text and binary files is also different. In case of a binary file, data is contained
as lines of text. An end of line marker is automatically added we indicate that the end of the line is reaches. The
fwrite() function of „C‟ allows you to write an end of line marker at the end of text in a text file. However in case of
a binary file, the data is not separated and hence no end of line marker is written at the end of the text when and
writing data to a binary file using fwirte() function. The reading process for text and binary files is also is also
different. In case of text file, the data is separated into lines of text as it is read according to the position of the end of
the line marker. On the other hand, the data in a binary file is not separated into lines of text.

Weitere ähnliche Inhalte

Was ist angesagt? (19)

File Management in C
File Management in CFile Management in C
File Management in C
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
C programming file handling
C  programming file handlingC  programming file handling
C programming file handling
 
File handling in C
File handling in CFile handling in C
File handling in C
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File Management
File ManagementFile Management
File Management
 
Unit5
Unit5Unit5
Unit5
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
 
UNIT 10. Files and file handling in C
UNIT 10. Files and file handling in CUNIT 10. Files and file handling in C
UNIT 10. Files and file handling in C
 
Unit v
Unit vUnit v
Unit v
 
file management in c language
file management in c languagefile management in c language
file management in c language
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 
file handling1
file handling1file handling1
file handling1
 
1file handling
1file handling1file handling
1file handling
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
 
Satz1
Satz1Satz1
Satz1
 
File mangement
File mangementFile mangement
File mangement
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 

Ähnlich wie Unit 8 (20)

File handling in c
File handling in cFile handling in c
File handling in c
 
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdfEASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
EASY UNDERSTANDING OF FILES IN C LANGUAGE.pdf
 
File in c
File in cFile in c
File in c
 
File handling-c
File handling-cFile handling-c
File handling-c
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdfAdvance C Programming UNIT 4-FILE HANDLING IN C.pdf
Advance C Programming UNIT 4-FILE HANDLING IN C.pdf
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
File handling C program
File handling C programFile handling C program
File handling C program
 
File handling
File handlingFile handling
File handling
 
PPS-II UNIT-5 PPT.pptx
PPS-II  UNIT-5 PPT.pptxPPS-II  UNIT-5 PPT.pptx
PPS-II UNIT-5 PPT.pptx
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
 
File management
File managementFile management
File management
 
INput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptxINput output stream in ccP Full Detail.pptx
INput output stream in ccP Full Detail.pptx
 
File Organization
File OrganizationFile Organization
File Organization
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
 

Kürzlich hochgeladen

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

Unit 8

  • 1. UNIT 8 FILES File: A file is a place on the disk where a group of related data is stored. It is a permanent named unit that holds large volume of related data. It stores the data in secondary or auxiliary memory such as CDs, tapes and Hard disk. Hence, it is permanently stored memory. It is used to support the volatile nature of main memory. That is, when the system is shutdown, main memory looses the data and relies on secondary storage. When the system is switched on, main memory gets loaded with the data from the files. In addition to reading data from files, main memory will also write data into the files if it has made any changes to the data. In addition to reading data from files, main memory will also write data into the files if it has made any changes to the data. A buffer is such a temporary storage used for data transfers between main memory and secondary memory. File name: The file name is typically a string of characters. The file names can be “Data.txt”, ”mul.txt”, ”prg.c” or “Div.n” etc. A file name may contain two parts, a primary name and optional period with the extension. Ex: input.data, student.c etc File Information Table: In order to read or write a file in a program, certain information is required like file name, the location of the current character of the file etc. such information is stored in a predefined structure called file information table. This table is defined by “stdio.h” header file with “FILE” as its identifier. Hence, whenever a file is to be read or written, it is declared using the „FILE‟ type. Stream: “A stream is a flow of data bytes between a program and files.” A stream can be either a source of data (in case of a write operation). The write-end of a stream is called an input stream. The read-end of a stream is called an output stream. „C‟ language provides stream as the basic for all types of operations on files. The files generally are stored on some secondary storage. Such as disks, tapes etc. When a „C‟ program has to operate on files it uses standard library functions declared in header file stdio.h. All those functions either read or write data from or to the file through streams i.e., All input output related functions use streams as the communication. Redirecting I/O to a File : Both read and write file operations can be easily performed under many operating systems. If we want to write program results into a file, then command. prog > data, will execute the program with the name „prog‟ and writes the output to the file called data. We can redirect input from file also Ex : reverse <file1 This command takes the value from file „file1‟ and redirects to program called „reverse‟. We can redirect input and output to the program at the same time. Ex : reverse < number > data This command reads all the input for the program „reverse‟ from file „number‟ and write all the output to the file „data‟. End of file : And end-file condition exists when the final piece of data has been read from a file. Attempting to read after end of file might cause the program to terminate with an error (or) it might cause the program to go into an infinite loop. EOF() function is defined in the standard I/O include file <stdio.h> Special functions for working with Files Following are the main steps involved file processing 1. Opening File 2. Processing a File 3. Closing a File
  • 2. Defining file Pointer : A file is opened, accessed, closed through a file pointer. A file pointer is declared using „FILE‟, which is defined in <stdio.h>. FILE *name of the file pointer; Ex : FILE * fp; The fopen () function : Before doing any I/O operation on a file, the file must be opened. To open a file, we must specify the name of the file. The function fopen() is used to open (or) create a file. The general format of the fopen() function is fopen (“filename”, “mode); The function fopen () takes two arguments i) filename ii) system type operation i.e. mode Modes : A file may be opened in one of the following modes „w‟ mode This mode opens the file in write mode. Only file write is possible „r‟ mode This mode opens the file in read mode. Only file read is possible „a‟ mode This mode opens the file in append mode. In this mode data is added from the end of the existing data. „w+‟ mode Write mode with read option „r+‟ mode read mode with write option „a+‟ mode append mode with write option If we add „b‟ i.e „wb‟, „rb‟ etc., the file will be opened in binary mode for corresponding system operation. Ex : fp =fopen(“student.dat”, “w”); This statement opens a file “student.dat” in write mode and return the file address into file pointer „fp‟. The fopen() function opens the file, but if file is not existing the function creates a file. The getc() and putc() Functions : getc() : The getc() function inputs one character from a specified file stream. The syntax of getc() is : char getc (FILE * stream); Ex : char flag ; flag = get c (fp); This statement inputs a character from the file defined by fp. putc() : The putc() function outputs one character to the file stream represented by the specified file pointer. The syntax of putc() is : char putc (char ch, FILE * stream); Ex : putc (flag, fp); This statement writes the character „flag‟ to file pointed by „fp‟ The fclose () Function : When operations are completed on a file, it is good habit to close the file. When a program terminates normally, the system automatically closes any open files. It is generally better programming practice to close a file, as soon as we are done with it. fclose() function is used to close an opened file. The general format of fclose() is :
  • 3. fclose (filepointer); The argument to the fclose () is filepointer. Ex : fclose (fp); Closes the file defined by the file pointer „fp‟. The feof () Function : The feof() function checks the file position indicator to determine if the end of the file associated with stream has been reached. A nonzero value is returned for the end of the file, otherwise zero is returned. Syntax : feof (filepointer); Ex : while (! feof(fp)) { ..... ..... } The loop continues till end of the file. Ex : Following program explains copying contents of one file to another file : #include <stdio.h> main () { FILE *fp1, *fp2; char flag; fp1 = fopen (“Charadata1.data”, “r”); fp2 = fopen (“Charadata2.data”, “w”); while (! feof(fp)) { flag = getc (fp1); putc (flag, fp2); } fclose (fp1); fclose (fp2); getch();} In order to run above program the file chardata1.data should exists i.e. it should be created. The while loop copies the contents of file chardata1.data to chardata2.data. The fprintf () Function : The fprintf () is like printf ( ) except that this function writes the data to a specified file. This function takes an additional parameter, which is the FILE pointer that identifies the file to which data is to be written. Ex : fprintf (fp, “programming in c is fun n”); This statement writes the specified string into a file identified by the file pointer fp. The fscanf () Function : This function is used to read data from a file. The general format is fscanf (File pointer, “control characters”, & variablenames); Ex : fscanf (fp, “%d %f”, &a, &b); This statement reads an integer and float value assigns them to variables „a‟ and „b‟ respectively from a file identified by „fp‟. The fgets () Function :
  • 4. fgets() function is used to read a string from a file. The syntax of fgets() is as shown below: char *fgets(char *str, int num, FILE * stream); The fgets() function reads upto „num‟ characters from a file pointed by „stream‟ and places them into the character array pointed to by „str‟. Characters are read until a new line or until the specified limit is reached. Ex: fgets(str, 20, fp); This statement reads a string of length 20 pointed by filepointer „fp‟ to the character array „str‟. The fputs () Function : fputs() function is used to write a string on to the file. The syntax of the fputs() frunction is : fputs(char *str, FILE *stream); fputs() function writes the contents of the string pointed to by „str‟ to the specified stream. The null terminator is not written. Ex: fputs(str, fp); This statement writes the string „str‟ in to the file pointed by the file pointer fp. Ex : Programs to perform file operations using fgets() and fputs : # include “stdio.h” main () { FILE *fp; int n; char str[5]; printf(“n Enter No.of Strings :”); scanf(“%d”, &n); fp =fopen(“Names.dat”, “w”); for (i=1; i<=n; i++) { scanf (“%s”, str); fputs (str, fp); } fclose (fp);} The above program creates a file “names.dat” and writes „n‟ strings using fputs. main() { FILE *fp; char str[5]; fp = fopen (“names.dat”, “r”); while (!feof(fp)) { fgets(str, 5, fp); prints(“n%s”, str); } fclose (fp);} This program opens the file names.dat in read mode. The while loop reads string by string and display them on the screen, until end of the file. stdin, stdout, stderr : In „C‟ when the program begins execution, the following three files will be automatically opened.
  • 5. 1. Standard input, stdin 2. Standard output, stdout 3. Standard error, stderr By default, the stdin, stdout, stderr refer to user‟s console. This means that whenever a program expects input from the standard input, it receives that input from stdin. A program that writes to the standard output prints it data to stdout. Any error messages that are generated by the library routines are sent to stderr. fwrite() : This function is used to write a record (or) structure to the file. fwrite(&s, sizeof(structure),1,fp); Structure „s‟ will be stored into the file pointed by fp. fread() : This function is used to read a record / structure from the file. fread(&s, sizeof(structure),1,fp); Structure „s‟ will be read from the file pointed by fp. ftell () : This function is used to locate the position of the file pointer. It returns a long integer. Ex : Long int L; L=ftell (fp); fseek() : This function is used to set the file pointer to the desired position. fseek( fp, ibytes, pos); rewind() : This function sets the file pointer to the initial position of the file. Ex : rewind(fp). The exit() Function : To explicitly terminate a program, the function exit () will be called. exit (n) ; has the effect of terminating the current program. Any open files are automatically closed by the system. The integer „n „ is called exit status. Renaming and Removing Files : rename () function is used to rename the existing file. Ex : rename (“tempfile”, “database”); This statement renames the file “tempfile” to “database”. remove () function deletes the file specified by its argument. Ex : remove (“tempfile”); This statement deletes the file “tempfile Random Accessing Files : Generally a file is accessed sequentially. For random access data „C‟ supports different functions. ftell () : This function is used to locate the position of the file pointer. It returns a long integer. Ex : Long int L; L=ftell (fp); fseek() : This function is used to set the file pointer to the desired position. fseek( fp, ibytes, pos); rewind() : This function sets the file pointer to the initial position of the file. Ex : rewind(fp). To access the record randomly : i. Read the present file pointer position using ftell().
  • 6. ii. Set the file pointer to initial position using rewind(). iii. Set the file pointer to required position by using fseek(). Text files and Binary files: The text files are those files that contain letters, digits and symbols. A text file mainly contains characters coded from the ASCII character set. On the other hand, a binary file is a file that uses all eight bits of bytes for storing information. The text file is mainly in a form, which can read and understood by human beings. The binary file, on the other hand, is generally in a form, which can be interpreted and understood by a computer system. In contract to a binary file, the text file uses only seven bits allowing the eighth bit to be zero. In a computer system, all executable or .exe files. Dynamic Link Library(DLL) and many database files are stored as binary files. One of the differences between text and binary file is that the data contained in the text file can be read by a word processor while the binary file data cannot be read by a word processor. In „C‟ when you access a file for reading and writing then you can choose to open the file as binary or text file. „C‟ programming language provides the fopen() function to open a file in a specific mode. When we specify the mode with the fopen() function, you can choose to open the file in a read mode for reading, write mode for appending data to the end of a file. It is also determines at the time of opening a file using fopen() function that whether the file should be opened as a binary or text file. The „C‟ programming language provides different read, write and append modes for reading and writing to text and binary files. If you use the following modes with fopen() function then the file is opened as a text file.  r: this mode allows you to open a file as a text file for reading data from it.  w: this mode allows you to open a file as a text file for writing data to it.  a: this mode allows you to open a file as a text file for appending data at the end of the file.  r+: this mode allows you to open a file as a text file for reading data as well as writing data to a file.  w+: this mode allows you to open a file as a text file for writing data as well as reading data from a file.  a+: this mode allows you to open a file as text file for both reading and writing data to a file. We should add „b‟ for binary file with the r,w and a modes to a file as a binary file, you can use the following modes:  rb: this mode allows you to open a file as a binary file for reading data from it.  wb: this mode allows you to open a file as a binary file for writing data to it.  ab:this mode allows you to open a file as a binary file for appending data at the end of the file.  rb+: this mode allows you to open a file as a binary file for reading as well as writing data to a file.  wb+: this mode allows you to open a file as a binary file for writing data as well as reading data from a file.  ab+: this mode allows you to open a file as binary file for both reading and writing data to a file. In „C‟, the process for writing of text and binary files is also different. In case of a binary file, data is contained as lines of text. An end of line marker is automatically added we indicate that the end of the line is reaches. The fwrite() function of „C‟ allows you to write an end of line marker at the end of text in a text file. However in case of a binary file, the data is not separated and hence no end of line marker is written at the end of the text when and writing data to a binary file using fwirte() function. The reading process for text and binary files is also is also different. In case of text file, the data is separated into lines of text as it is read according to the position of the end of the line marker. On the other hand, the data in a binary file is not separated into lines of text.