SlideShare ist ein Scribd-Unternehmen logo
1 von 36
www.eshikshak.co.in
ï‚Ą A collection of data or information that has a are
  stored on computer known as file
ï‚Ą A file is collection of bytes stored on a secondary
  storage device.
ï‚Ą There are different types of file
     Data files
     Text files
     Program files
     Directory files
ï‚Ą Different types of file stored different types of
  information
ï‚ĄA   file has a beginning and an end, it has a
  current position, typically defined as on many
  bytes from the beginning.
ï‚Ą You can move the current position to any
  other point in file.
ï‚Ą A new current position can be specified as an
  offset from the beginning the file.
ï‚ĄA  stream is an abstract representation of any
  external source or destination for data, so the
  keyword, the command line on your display
  and files on disk are all examples of stream.
ï‚Ą ‘C’ provides numbers of function for reading
  and writing to or from the streams on any
  external devices.
ï‚ĄA  stream is a series of bytes data flow from your
  program to a file or vice-versa.
ï‚Ą There are two formats of streams which are as
  follows
   Text Stream
     â–Ș It consists of sequence of characters, depending on the
       compilers
     â–Ș Each character line in a text stream may be terminated by a
       newline character.
     â–Ș Text streams are used for textual data, which has a consistent
       appearance from one environment to another or from one
       machine to another
ï‚ĄA  stream is a series of bytes data flow from
  your program to a file or vice-versa.
ï‚Ą There are two formats of streams which are
  as follows
  Binary Stream
   â–Ș It is a series of bytes.
   â–Ș Binary streams are primarily used for non-textual data,
     which is required to keep exact contents of the file.
ï‚ĄA    text file can be a stream of characters that
  a computer can process sequentially.
ï‚Ą It is processed only in forward direction.
ï‚Ą It is opened for one kind of operation
  (reading, writing, or appending) at any give
  time.
ï‚Ą It can read only one character at a time.
ï‚Ą A binary file is collection of bytes.
ï‚Ą In ‘C’ both a both a byte and a character are
  equivalent.
ï‚Ą A binary file is also referred to as a character
  stream, but there are two essential
  differences.
ï‚Ą A file is identified by its name.
ï‚Ą This name is divided into two parts
   File Name
    â–Ș It consists of alphabets and digits.
    â–Ș Special characters are also supported, but it depends on
      operating system.
   Extension
    â–Ș It describe the file type
ï‚Ą Before opening a file, we have to create a file
 pointer.


ï‚Ą FILE   structure is defined in the “stdio.h”
  header file.
ï‚Ą It stores the complete information about file.
   Name of file, mode it is opened in, starting buffer address,
    a character pointer that points to the character being read.
ï‚Ą To  perform any operation (read or write) the
  file has to be brought into memory from the
  storage device.
ï‚Ą Thus, bringing the copy of file from disk to
  memory is called opening the file.
Mode   Meaning
r       Open a text file for reading only. If the file doesn’t exist, it returns null.
w       Opens a file for writing only.
        If file exists, than all the contents of that file are destroyed and new fresh blank file
       is copied on the disk and memory with same name
        If file dosen’t exists, a new blank file is created and opened for writing.
        Returns NULL if it is unable to open the file
a       Appends to the existing text file
        Adds data at the end of the file.
        If file doesn’t exists then a new file is created.
        Returns NULL if it is unable to open the file.
rb      Open a binary file for reading
wb      Open a binary file for reading
ab      Append to a binary file
r+      Open a text file for read/write
w+      Opens the existing text file or Creates a text file for read/write
Mode   Meaning
a+      Append or create a text file for read/write
r+b     Open a binary file for read/write
w+b     Create a binary file for read/write
a+b     Append a binary file for read/write
ï‚Ą fopen()  function, like all the file-system
  functions, uses the head file stdio.h
ï‚Ą The name of the file to open is pointed to by
  fname
ï‚Ą The string pointed at for mode determined
  how the file may be accessed.
FILE *fp;
if(fp = fopen(“myfile”,”r”)) == NULL)
{
  printf(“Error opening a file”);
    exit(1);
}
ï‚Ą When we want to read contents from existing
  file, then we require to open that file into
  read mode that means “r” mode
ï‚Ą To read file follow the below steps
 1.   Initialize the file variable
 2.   Open a file in read mode
 3.   Accept information from file
 4.   Write it into the output devices
 5.    Repeat steps 3 and 4 till file is not ends
 6.   Stop procedure
#include<stdio.h>
void main()
{
  FILE *fp;
  char ch;
  fp=fopen(“clear.c”,”r”);
  if(fp==NULL)
       print(“Unable to open clear.c”);
  else
  {
      do
      {
             ch = getc(fp);
             putchar(ch);
      }while(ch!=EOF);
     fclose(fp);
  }
}
ï‚Ą A file contains a large amount of data.
ï‚Ą We some times cannot detect the end of file.
ï‚Ą Intext file, a special character EOP denotes
  the end-of-file
ï‚Ą To   close a file and dis-associate it with a
  stream, use fclose() function.
ï‚Ą It returns 0 if successful else returns EOP if
  error occurs.
ï‚Ą The fcloseall() closes all the files opened
  previously.
#include<stdio.h>
void main()
{
  FILE *fp;
  char ch;
  fp=fopen(“clear.c”,”r”);
  if(fp==NULL)
       print(“Unable to open clear.c”);
  else
  {
      do
      {
             ch = getc(fp);// gets the character from file
             putchar(ch);
      }while(ch!=EOF);
     fclose(fp);
  }
}
ï‚Ą We  can add contents to the existing file
  whenever it is required.
ï‚Ą Perform the following steps
 1. Initialize the variable
 2. Open a file in append mode
 3. Accept information from user
 4. Write it into the file
 5. Repeat steps 3 and 4 according to user’s
    choice
 6. Stop procedure
ï‚Ą char *fgets(char *str, int n, FILE *fptr);
   This statement reads character from the stream
   fptr into to the character array ‘str’ until a new line
   character is read or end of file is reached or n-1
   characters have been read.

ï‚Ą fputs(const char *str, FILE *fptr);
   This statement writes to the stream fptr except
   the terminating null character of string ‘str’
#include<stdio.h>
void main()
{
   FILE *fp;
  char line[280];int ch, i=0;
   fp=fopen(“a.dat”,”a”);
  if(fp==NULL)
       print(“Unable to open clear.c”);
   else {
        do{
                do{
                        line[i++] = getchar();
                }while(line[i-1]!=‘*’);
                fputs(line, fp);//Writes string to the file
                i=0;
                printf(“nPress 1 to continue”);
                scanf(“%d”,&ch);
        }while(ch==1);
     fclose(fp);
      printf(“File is successfully created”);
   }
}
ï‚Ą We can modify the files in two ways
   Serial access
   Random access
ï‚Ą Generally all the text files are considered to be
  sequential files because lines of text files or
  records of the formatted files are not equal.
ï‚Ą So address of each record is un predictable
ï‚Ą Whereas, in random access file all the record
  length are same.
ï‚Ą   To modify the file open the file with read and write mode
ï‚Ą   “r+” or “w+” or “a+”
ï‚Ą   Generally “r+” mode is use for this purpose
    1.        Initialize the variable
    2.        Open a file in read/write mode
    3.        Read information from file
    4.        Print it
    5.        Check whether the information is right or wrong?
         1.    If Wrong, accep correct information from user, move the file
               pointer to the start of record or information. Re-write it
               that means new information into the file.
         2.    If not wrong, go to step 6.
    6.        Repeat steps 3 to 6 till file is not end.
    7.        Stop procedure
ï‚Ą   To modify the file open the file with read and write mode
ï‚Ą   “r+” or “w+” or “a+”
ï‚Ą   Generally “r+” mode is use for this purpose
    1.        Initialize the variable
    2.        Open a file in read/write mode
    3.        Read information from file
    4.        Print it
    5.        Check whether the information is right or wrong?
         1.    If Wrong, accep correct information from user, move the file
               pointer to the start of record or information. Re-write it
               that means new information into the file.
         2.    If not wrong, go to step 6.
    6.        Repeat steps 3 to 6 till file is not end.
    7.        Stop procedure
#include<stdio.h>
struct stock
{
   int itid, qty;
   char n[100];
   float rate;
}it;

void main()
{
   FILE *fp; int ch; int r = 0;
   fp = fopen(“item.c”, “r+”);
   if(fp==NULL)
   {
       printf(“Unable to open item.c”);
   }
}
else{
   do{
         fread(&it, sizeof(it),1,fp);
         printf(“n%d %s %d %f”, it.itid, it.n, it.qty, it.rate);
         printf(“n Press 1 to change it?”);
         scanf(“%d”,&ch);

        if(ch==1)
        {
                 printf(“n Enter Itemid ItemName Quantity & Price”);
                 scanf(“%d%s%d%f”, &it.itid, it.n, &it.qty, &it.rate);
                 fseek(fp, r*sizeof(it), 0);
                 fwrite(&it, sizeof(it),1,fp);
        }r++;
    }while(!feof(fp));
    fclose(fp);
}
}
fseek(FILE *fptr, long offset, int reference)
ï‚Ą Moves the pointer from one record to
  another.
ï‚Ą The first argument is the file pointer
ï‚Ą The second argument tells the compiler by
  how many bytes the pointer should be moved
  from a particular position.
ï‚Ą The third argument is the reference from
  which the pointer should be offset.
fseek(FILE *fptr, long offset, int reference)
ï‚Ą The third argument is the reference from
  which the pointer should be offset.
  SEEK_END moves the pointer from end of file
  SEEK_CUR moves the pointer from the current
   position
  SEEK_SET moves the pointer from the beginning
   of the file
fseek(FILE *fptr, long offset, int reference)
ï‚Ą Example
   fseek(fptr, size, SEEK_CUR) sets the cursor ahead
    from current position by size bytes
   fseek(fptr, -size, SEEK_CUR) sets the cursor back
    from current position by size bytes
   fseek(fptr, 0, SEEK_END) sets cursor to the end of
    the file
   fseek(fptr, 0, SEEK_SET) sets cursor to the
    beginning of the file
Mesics lecture   files in 'c'
Mesics lecture   files in 'c'

Weitere Àhnliche Inhalte

Was ist angesagt?

C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
verisan
 

Was ist angesagt? (20)

C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
Files in c++
Files in c++Files in c++
Files in c++
 
Python: Manager Objects
Python: Manager ObjectsPython: Manager Objects
Python: Manager Objects
 
Stacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti AroraStacks & Queues By Ms. Niti Arora
Stacks & Queues By Ms. Niti Arora
 
Stacks in c++
Stacks in c++Stacks in c++
Stacks in c++
 
C++ Pointers And References
C++ Pointers And ReferencesC++ Pointers And References
C++ Pointers And References
 
358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10358 33 powerpoint-slides_10-trees_chapter-10
358 33 powerpoint-slides_10-trees_chapter-10
 
CPU INPUT OUTPUT
CPU INPUT OUTPUT CPU INPUT OUTPUT
CPU INPUT OUTPUT
 
File in C language
File in C languageFile in C language
File in C language
 
List Data Structure
List Data StructureList Data Structure
List Data Structure
 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
 
Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...Python File Handling | File Operations in Python | Learn python programming |...
Python File Handling | File Operations in Python | Learn python programming |...
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Unit 2 python
Unit 2 pythonUnit 2 python
Unit 2 python
 
Hashing PPT
Hashing PPTHashing PPT
Hashing PPT
 
Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3
 
Linux basic commands
Linux basic commandsLinux basic commands
Linux basic commands
 
Debugging Python with Pdb!
Debugging Python with Pdb!Debugging Python with Pdb!
Debugging Python with Pdb!
 
Expression evaluation
Expression evaluationExpression evaluation
Expression evaluation
 

Andere mochten auch

structure and union
structure and unionstructure and union
structure and union
student
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02
eShikshak
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
eShikshak
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computing
eShikshak
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
chidabdu
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
eShikshak
 
Html text and formatting
Html text and formattingHtml text and formatting
Html text and formatting
eShikshak
 
Linked list
Linked listLinked list
Linked list
eShikshak
 

Andere mochten auch (20)

structure and union
structure and unionstructure and union
structure and union
 
Bluetooth1
Bluetooth1Bluetooth1
Bluetooth1
 
Presentation
PresentationPresentation
Presentation
 
Lecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.pptLecture15 comparisonoftheloopcontrolstructures.ppt
Lecture15 comparisonoftheloopcontrolstructures.ppt
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 
Html phrase tags
Html phrase tagsHtml phrase tags
Html phrase tags
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Lecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operatorsLecture 7 relational_and_logical_operators
Lecture 7 relational_and_logical_operators
 
Mesics lecture 3 c – constants and variables
Mesics lecture 3   c – constants and variablesMesics lecture 3   c – constants and variables
Mesics lecture 3 c – constants and variables
 
Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02Lecture7relationalandlogicaloperators 110823181038-phpapp02
Lecture7relationalandlogicaloperators 110823181038-phpapp02
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Unit 1.3 types of cloud
Unit 1.3 types of cloudUnit 1.3 types of cloud
Unit 1.3 types of cloud
 
Mesics lecture 7 iteration and repetitive executions
Mesics lecture 7   iteration and repetitive executionsMesics lecture 7   iteration and repetitive executions
Mesics lecture 7 iteration and repetitive executions
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Unit 1.1 introduction to cloud computing
Unit 1.1   introduction to cloud computingUnit 1.1   introduction to cloud computing
Unit 1.1 introduction to cloud computing
 
Algorithm chapter 11
Algorithm chapter 11Algorithm chapter 11
Algorithm chapter 11
 
Lecture19 unionsin c.ppt
Lecture19 unionsin c.pptLecture19 unionsin c.ppt
Lecture19 unionsin c.ppt
 
Unit 1.2 move to cloud computing
Unit 1.2   move to cloud computingUnit 1.2   move to cloud computing
Unit 1.2 move to cloud computing
 
Html text and formatting
Html text and formattingHtml text and formatting
Html text and formatting
 
Linked list
Linked listLinked list
Linked list
 

Ähnlich wie Mesics lecture files in 'c'

Presentation of file handling in C language
Presentation of file handling in C languagePresentation of file handling in C language
Presentation of file handling in C language
Shruthi48
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
Amarjith C K
 
Unit5
Unit5Unit5
Unit5
mrecedu
 

Ähnlich wie Mesics lecture files in 'c' (20)

Presentation of file handling in C language
Presentation of file handling in C languagePresentation of file handling in C language
Presentation of file handling in C language
 
Unit-VI.pptx
Unit-VI.pptxUnit-VI.pptx
Unit-VI.pptx
 
File handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptxFile handling in C hhsjsjshsjjsjsjs.pptx
File handling in C hhsjsjshsjjsjsjs.pptx
 
File management
File managementFile management
File management
 
Module 03 File Handling in C
Module 03 File Handling in CModule 03 File Handling in C
Module 03 File Handling in C
 
Module 5 file cp
Module 5 file cpModule 5 file cp
Module 5 file cp
 
Unit5 C
Unit5 C Unit5 C
Unit5 C
 
Lecture 20 - File Handling
Lecture 20 - File HandlingLecture 20 - File Handling
Lecture 20 - File Handling
 
file_handling_in_c.ppt
file_handling_in_c.pptfile_handling_in_c.ppt
file_handling_in_c.ppt
 
PPS PPT 2.pptx
PPS PPT 2.pptxPPS PPT 2.pptx
PPS PPT 2.pptx
 
File management
File managementFile management
File management
 
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
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
Satz1
Satz1Satz1
Satz1
 
Unit 8
Unit 8Unit 8
Unit 8
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
File handling
File handlingFile handling
File handling
 
Unit5
Unit5Unit5
Unit5
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Data Structure Using C - FILES
Data Structure Using C - FILESData Structure Using C - FILES
Data Structure Using C - FILES
 

Mehr von eShikshak

Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
eShikshak
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
eShikshak
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
eShikshak
 
Language processors
Language processorsLanguage processors
Language processors
eShikshak
 

Mehr von eShikshak (18)

Modelling and evaluation
Modelling and evaluationModelling and evaluation
Modelling and evaluation
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Datatypes in python
Datatypes in pythonDatatypes in python
Datatypes in python
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Introduction to e commerce
Introduction to e commerceIntroduction to e commerce
Introduction to e commerce
 
Chapeter 2 introduction to cloud computing
Chapeter 2   introduction to cloud computingChapeter 2   introduction to cloud computing
Chapeter 2 introduction to cloud computing
 
Unit 1.4 working of cloud computing
Unit 1.4 working of cloud computingUnit 1.4 working of cloud computing
Unit 1.4 working of cloud computing
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Mesics lecture 4 c operators and experssions
Mesics lecture  4   c operators and experssionsMesics lecture  4   c operators and experssions
Mesics lecture 4 c operators and experssions
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Lecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.pptLecture20 user definedfunctions.ppt
Lecture20 user definedfunctions.ppt
 
Lecture18 structurein c.ppt
Lecture18 structurein c.pptLecture18 structurein c.ppt
Lecture18 structurein c.ppt
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Lecture13 control statementswitch.ppt
Lecture13 control statementswitch.pptLecture13 control statementswitch.ppt
Lecture13 control statementswitch.ppt
 
Lecturer23 pointersin c.ppt
Lecturer23 pointersin c.pptLecturer23 pointersin c.ppt
Lecturer23 pointersin c.ppt
 
Program development cyle
Program development cyleProgram development cyle
Program development cyle
 
Language processors
Language processorsLanguage processors
Language processors
 
Computer programming programming_langugages
Computer programming programming_langugagesComputer programming programming_langugages
Computer programming programming_langugages
 

KĂŒrzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

KĂŒrzlich hochgeladen (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 

Mesics lecture files in 'c'

  • 2. ï‚Ą A collection of data or information that has a are stored on computer known as file ï‚Ą A file is collection of bytes stored on a secondary storage device. ï‚Ą There are different types of file  Data files  Text files  Program files  Directory files ï‚Ą Different types of file stored different types of information
  • 3. ï‚ĄA file has a beginning and an end, it has a current position, typically defined as on many bytes from the beginning. ï‚Ą You can move the current position to any other point in file. ï‚Ą A new current position can be specified as an offset from the beginning the file.
  • 4. ï‚ĄA stream is an abstract representation of any external source or destination for data, so the keyword, the command line on your display and files on disk are all examples of stream. ï‚Ą ‘C’ provides numbers of function for reading and writing to or from the streams on any external devices.
  • 5. ï‚ĄA stream is a series of bytes data flow from your program to a file or vice-versa. ï‚Ą There are two formats of streams which are as follows  Text Stream â–Ș It consists of sequence of characters, depending on the compilers â–Ș Each character line in a text stream may be terminated by a newline character. â–Ș Text streams are used for textual data, which has a consistent appearance from one environment to another or from one machine to another
  • 6. ï‚ĄA stream is a series of bytes data flow from your program to a file or vice-versa. ï‚Ą There are two formats of streams which are as follows  Binary Stream â–Ș It is a series of bytes. â–Ș Binary streams are primarily used for non-textual data, which is required to keep exact contents of the file.
  • 7.
  • 8. ï‚ĄA text file can be a stream of characters that a computer can process sequentially. ï‚Ą It is processed only in forward direction. ï‚Ą It is opened for one kind of operation (reading, writing, or appending) at any give time. ï‚Ą It can read only one character at a time.
  • 9. ï‚Ą A binary file is collection of bytes. ï‚Ą In ‘C’ both a both a byte and a character are equivalent. ï‚Ą A binary file is also referred to as a character stream, but there are two essential differences.
  • 10.
  • 11.
  • 12. ï‚Ą A file is identified by its name. ï‚Ą This name is divided into two parts  File Name â–Ș It consists of alphabets and digits. â–Ș Special characters are also supported, but it depends on operating system.  Extension â–Ș It describe the file type
  • 13. ï‚Ą Before opening a file, we have to create a file pointer. ï‚Ą FILE structure is defined in the “stdio.h” header file. ï‚Ą It stores the complete information about file.  Name of file, mode it is opened in, starting buffer address, a character pointer that points to the character being read.
  • 14. ï‚Ą To perform any operation (read or write) the file has to be brought into memory from the storage device. ï‚Ą Thus, bringing the copy of file from disk to memory is called opening the file.
  • 15. Mode Meaning r  Open a text file for reading only. If the file doesn’t exist, it returns null. w  Opens a file for writing only.  If file exists, than all the contents of that file are destroyed and new fresh blank file is copied on the disk and memory with same name  If file dosen’t exists, a new blank file is created and opened for writing.  Returns NULL if it is unable to open the file a  Appends to the existing text file  Adds data at the end of the file.  If file doesn’t exists then a new file is created.  Returns NULL if it is unable to open the file. rb  Open a binary file for reading wb  Open a binary file for reading ab  Append to a binary file r+  Open a text file for read/write w+  Opens the existing text file or Creates a text file for read/write
  • 16. Mode Meaning a+  Append or create a text file for read/write r+b  Open a binary file for read/write w+b  Create a binary file for read/write a+b  Append a binary file for read/write
  • 17. ï‚Ą fopen() function, like all the file-system functions, uses the head file stdio.h ï‚Ą The name of the file to open is pointed to by fname ï‚Ą The string pointed at for mode determined how the file may be accessed.
  • 18. FILE *fp; if(fp = fopen(“myfile”,”r”)) == NULL) { printf(“Error opening a file”); exit(1); }
  • 19. ï‚Ą When we want to read contents from existing file, then we require to open that file into read mode that means “r” mode ï‚Ą To read file follow the below steps 1. Initialize the file variable 2. Open a file in read mode 3. Accept information from file 4. Write it into the output devices 5. Repeat steps 3 and 4 till file is not ends 6. Stop procedure
  • 20. #include<stdio.h> void main() { FILE *fp; char ch; fp=fopen(“clear.c”,”r”); if(fp==NULL) print(“Unable to open clear.c”); else { do { ch = getc(fp); putchar(ch); }while(ch!=EOF); fclose(fp); } }
  • 21. ï‚Ą A file contains a large amount of data. ï‚Ą We some times cannot detect the end of file. ï‚Ą Intext file, a special character EOP denotes the end-of-file
  • 22. ï‚Ą To close a file and dis-associate it with a stream, use fclose() function. ï‚Ą It returns 0 if successful else returns EOP if error occurs. ï‚Ą The fcloseall() closes all the files opened previously.
  • 23. #include<stdio.h> void main() { FILE *fp; char ch; fp=fopen(“clear.c”,”r”); if(fp==NULL) print(“Unable to open clear.c”); else { do { ch = getc(fp);// gets the character from file putchar(ch); }while(ch!=EOF); fclose(fp); } }
  • 24. ï‚Ą We can add contents to the existing file whenever it is required. ï‚Ą Perform the following steps 1. Initialize the variable 2. Open a file in append mode 3. Accept information from user 4. Write it into the file 5. Repeat steps 3 and 4 according to user’s choice 6. Stop procedure
  • 25. ï‚Ą char *fgets(char *str, int n, FILE *fptr);  This statement reads character from the stream fptr into to the character array ‘str’ until a new line character is read or end of file is reached or n-1 characters have been read. ï‚Ą fputs(const char *str, FILE *fptr);  This statement writes to the stream fptr except the terminating null character of string ‘str’
  • 26. #include<stdio.h> void main() { FILE *fp; char line[280];int ch, i=0; fp=fopen(“a.dat”,”a”); if(fp==NULL) print(“Unable to open clear.c”); else { do{ do{ line[i++] = getchar(); }while(line[i-1]!=‘*’); fputs(line, fp);//Writes string to the file i=0; printf(“nPress 1 to continue”); scanf(“%d”,&ch); }while(ch==1); fclose(fp); printf(“File is successfully created”); } }
  • 27. ï‚Ą We can modify the files in two ways  Serial access  Random access ï‚Ą Generally all the text files are considered to be sequential files because lines of text files or records of the formatted files are not equal. ï‚Ą So address of each record is un predictable ï‚Ą Whereas, in random access file all the record length are same.
  • 28. ï‚Ą To modify the file open the file with read and write mode ï‚Ą “r+” or “w+” or “a+” ï‚Ą Generally “r+” mode is use for this purpose 1. Initialize the variable 2. Open a file in read/write mode 3. Read information from file 4. Print it 5. Check whether the information is right or wrong? 1. If Wrong, accep correct information from user, move the file pointer to the start of record or information. Re-write it that means new information into the file. 2. If not wrong, go to step 6. 6. Repeat steps 3 to 6 till file is not end. 7. Stop procedure
  • 29. ï‚Ą To modify the file open the file with read and write mode ï‚Ą “r+” or “w+” or “a+” ï‚Ą Generally “r+” mode is use for this purpose 1. Initialize the variable 2. Open a file in read/write mode 3. Read information from file 4. Print it 5. Check whether the information is right or wrong? 1. If Wrong, accep correct information from user, move the file pointer to the start of record or information. Re-write it that means new information into the file. 2. If not wrong, go to step 6. 6. Repeat steps 3 to 6 till file is not end. 7. Stop procedure
  • 30. #include<stdio.h> struct stock { int itid, qty; char n[100]; float rate; }it; void main() { FILE *fp; int ch; int r = 0; fp = fopen(“item.c”, “r+”); if(fp==NULL) { printf(“Unable to open item.c”); } }
  • 31. else{ do{ fread(&it, sizeof(it),1,fp); printf(“n%d %s %d %f”, it.itid, it.n, it.qty, it.rate); printf(“n Press 1 to change it?”); scanf(“%d”,&ch); if(ch==1) { printf(“n Enter Itemid ItemName Quantity & Price”); scanf(“%d%s%d%f”, &it.itid, it.n, &it.qty, &it.rate); fseek(fp, r*sizeof(it), 0); fwrite(&it, sizeof(it),1,fp); }r++; }while(!feof(fp)); fclose(fp); } }
  • 32. fseek(FILE *fptr, long offset, int reference) ï‚Ą Moves the pointer from one record to another. ï‚Ą The first argument is the file pointer ï‚Ą The second argument tells the compiler by how many bytes the pointer should be moved from a particular position. ï‚Ą The third argument is the reference from which the pointer should be offset.
  • 33. fseek(FILE *fptr, long offset, int reference) ï‚Ą The third argument is the reference from which the pointer should be offset.  SEEK_END moves the pointer from end of file  SEEK_CUR moves the pointer from the current position  SEEK_SET moves the pointer from the beginning of the file
  • 34. fseek(FILE *fptr, long offset, int reference) ï‚Ą Example  fseek(fptr, size, SEEK_CUR) sets the cursor ahead from current position by size bytes  fseek(fptr, -size, SEEK_CUR) sets the cursor back from current position by size bytes  fseek(fptr, 0, SEEK_END) sets cursor to the end of the file  fseek(fptr, 0, SEEK_SET) sets cursor to the beginning of the file