SlideShare a Scribd company logo
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.

More Related Content

What's hot

File Management in C
File Management in CFile Management in C
File Management in C
Paurav Shah
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
ٖFaiXy :)
 
File handling in c
File  handling in cFile  handling in c
File handling in c
thirumalaikumar3
 
C programming file handling
C  programming file handlingC  programming file handling
C programming file handling
argusacademy
 
File handling in C
File handling in CFile handling in C
File handling in C
Rabin BK
 
File Management
File ManagementFile Management
File Management
Ravinder Kamboj
 
File handling-dutt
File handling-duttFile handling-dutt
File handling-dutt
Anil 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
Ashim Lamichhane
 
file management in c language
file management in c languagefile management in c language
file management in c language
chintan makwana
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
Harish Kamat
 
file handling1
file handling1file handling1
file handling1student
 
Unit 5 dwqb ans
Unit 5 dwqb ansUnit 5 dwqb ans
Unit 5 dwqb ans
Sowri Rajan
 
File mangement
File mangementFile mangement
File mangement
Jigarthacker
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
Md. Imran Hossain Showrov
 

What's hot (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
 

Similar to Unit 8

File handling in c
File handling in cFile handling in c
File handling in c
aakanksha s
 
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
sudhakargeruganti
 
File in c
File in cFile in c
File in c
Prabhu Govind
 
File handling-c
File handling-cFile handling-c
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
Sandeepbhuma1
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
Mehul Desai
 
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
sangeeta borde
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
yndaravind
 
Chapter 13.1.10
Chapter 13.1.10Chapter 13.1.10
Chapter 13.1.10patcha535
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
yuvrajkeshri
 
File handling C program
File handling C programFile handling C program
File handling C program
Thesis Scientist Private Limited
 
File handling
File handlingFile handling
File handling
Ans Ali
 
PPS-II UNIT-5 PPT.pptx
PPS-II  UNIT-5 PPT.pptxPPS-II  UNIT-5 PPT.pptx
PPS-II UNIT-5 PPT.pptx
VenkataRangaRaoKommi1
 
VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5VIT351 Software Development VI Unit5
VIT351 Software Development VI Unit5
YOGESH SINGH
 
File management
File managementFile management
File management
sumathiv9
 
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
AssadLeo1
 
File Organization
File OrganizationFile Organization
File Organization
RAMPRAKASH REDDY ARAVA
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
Amarjith C K
 
Programming in C Session 4
Programming in C Session 4Programming in C Session 4
Programming in C Session 4
Prerna Sharma
 

Similar to 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
 

Recently uploaded

Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
nkrafacyberclub
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
RinaMondal9
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
Pierluigi Pugliese
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 

Recently uploaded (20)

Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptxSecstrike : Reverse Engineering & Pwnable tools for CTF.pptx
Secstrike : Reverse Engineering & Pwnable tools for CTF.pptx
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Free Complete Python - A step towards Data Science
Free Complete Python - A step towards Data ScienceFree Complete Python - A step towards Data Science
Free Complete Python - A step towards Data Science
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024By Design, not by Accident - Agile Venture Bolzano 2024
By Design, not by Accident - Agile Venture Bolzano 2024
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 

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.