SlideShare a Scribd company logo
1 of 15
Download to read offline
C             Programming
                       Language
               By:
    Yogendra Pal
yogendra@learnbywatch.com
  Dedicated to My mother and Father
t
                                                               y
         Keep your notebook with you.

Write important point and questions that comes in your mind

     Solve Mind band exercise.


                                                               C
                                       Rewind when not clear


              Ask Questions by call or SMS or by mail


Keep Watching Keep Learning

THIS IS FILE I/O


                                   2
File…
•   Files are used to store data to disks.
•   Data on disk is permanent.
•   Variables are stored on RAM only.
•   Data on RAM can not be accessed later.




                         3
File…
• File : Group of related records.
                     Sonu         001          04/02
                     Amit         002          09/05
                                                         File
                     Asif         003          10/09
                     Hari         004          30/11

                     Sonu          001           04/02          Record

                     Sonu           Field

                     1001001            byte

                     1      bit


                             4
Files in C
• A file is a sequence of bytes.
  – File ends with End Of File (EOF) marker.
• Streams are used to communicate with file.

                       stream



                            5
File
• Use FILE pointer to access a file.
          FILE *fp;

• fopen() library function is used to open a file.
   – fopen( file_path, open_mode);
   – Returns NULL if file can not open.
• fclose() library function is used to close a file.
   – fclose(file_pointer);


                               6
File open mode
Mode   Description
  r    Open a file for reading.
  w    Create a file for writing. If the file already exists, discard the current contents.
  a    Append; open or create a file for writing at end of file.
 r+    Open a file for update (reading and writing).
 w+    Create a file for update. If the file already exists, discard the current contents.
 a+    Append; open or create a file for update; writing is done at the end of the file.
 rb    Open a file for reading in binary mode.
 wb    Create a file for writing in binary mode. If the file already exists, discard the current contents.
 ab    Append; open or create a file for writing at end of file in binary mode.
 rb+   Open a file for update (reading and writing) in binary mode.
 wb+   Create a file for update in binary mode. If the file already exists, discard the current contents.
 ab+   Append; open or create a file for update in binary mode; writing is done at the end of the file.



                                                         7
Read / Write a file
•   fgetc() : reads one character from a file.
•   fputc() : write one character to a file.
•   fgets() : read a line (Srring) from a file.
•   fputs() : Write a line to a file.
•   fprintf() : used to print to a file.
•   fscanf() : used to read from a file.
•   feof() : return true if EOF find.
•   rewind() : set file position pointer to the start.
                            8
Problem
• Write a program to read a file and print it on
  monitor.
• Write a program to create a copy of a file.
• Modify first problem: stop printing if a ‘z’
  occurs in file.
• Write a program to create a file.


                        9
Sequential Access File
• Write or Read data sequentially.
• Data can not modify without destroying.
• Fields can vary in size.
      fopen(“one.dat”, “w”);




                               10
Random Access File
• Access any record without searching through
  other records.
• Data can be inserted without destroying other
  data.
• Implemented using fixed length records.
      fopen(“one.dat”, “wb”);

• Also known as binary file.

                                11
Random Access File…
• fwrite() : used to write to binary file.
• fread() : read from binary file.
• fseek(): Used to set the file position pointer to
  a specific location.
• Operation sequention.
  – Open/Create->seek->Write.
  – Open->seek->Read.


                         12
fwrite() / fread()
• fwrite(&data,sizeof(data),1,fp);
  •   data : data that you want to write to file.
  •   sizeof(data) : number of bytes to write.
  •   1 : for arrays, number of elements to write.
  •    fp : file to write.
– fread(&data,sizeof(data),1,fp);
  •   &data : variable to store data.
  •   sizeof(data) : number of bytes to read.
  •   1 : for arrays, number of elements to read.
  •    fp : file from read.

                                 13
fseek()
• fseek(fp,offset,symbolic_constant);
  – fp : File pointer.
  – offset : location of file position pointer.
  – symbolic_constant : specify where in file we are
    reading from.
     • SEEK_SET: beginning of file.
     • SEEK_CUR: current location in file.
     • SEEK_END: end of file.


                               14
To get complete benefit of this tutorial solve all the quiz on
                   www.learnbywatch.com

          For any problem in this tutorial mail me at
                yogendra@learnbywatch.com
                    with the subject “C”

                 For Other information mail at
                   info@learnbywatch.com


                 Keep Watching Keep Learning
NOW YOU ARE READY TO LEARN C++ AND JAVA

More Related Content

Similar to File io

Similar to File io (20)

File handing in C
File handing in CFile handing in C
File handing in C
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
File handling in C by Faixan
File handling in C by FaixanFile handling in C by Faixan
File handling in C by Faixan
 
file_c.pdf
file_c.pdffile_c.pdf
file_c.pdf
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
 
Python file handling
Python file handlingPython file handling
Python file handling
 
Engineering Computers L34-L35-File Handling.pptx
Engineering Computers L34-L35-File Handling.pptxEngineering Computers L34-L35-File Handling.pptx
Engineering Computers L34-L35-File Handling.pptx
 
03-01-File Handling python.pptx
03-01-File Handling python.pptx03-01-File Handling python.pptx
03-01-File Handling python.pptx
 
File handling in c
File handling in cFile handling in c
File handling in c
 
File structure
File structureFile structure
File structure
 
Topic - File operation.pptx
Topic - File operation.pptxTopic - File operation.pptx
Topic - File operation.pptx
 
637225560972186380.pdf
637225560972186380.pdf637225560972186380.pdf
637225560972186380.pdf
 
File Handling
File HandlingFile Handling
File Handling
 
File Handling
File HandlingFile Handling
File Handling
 
File Management
File ManagementFile Management
File Management
 
FILES IN C
FILES IN CFILES IN C
FILES IN C
 
File management
File managementFile management
File management
 
chapter-4-data-file-handlingeng.pdf
chapter-4-data-file-handlingeng.pdfchapter-4-data-file-handlingeng.pdf
chapter-4-data-file-handlingeng.pdf
 
File Handling in C Programming for Beginners
File Handling in C Programming for BeginnersFile Handling in C Programming for Beginners
File Handling in C Programming for Beginners
 

More from Learn By Watch

Demodulation of fm pll detector
Demodulation of fm pll detectorDemodulation of fm pll detector
Demodulation of fm pll detectorLearn By Watch
 
Demodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorDemodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorLearn By Watch
 
In direct method of fm generation armstrong method
In direct method of fm generation armstrong methodIn direct method of fm generation armstrong method
In direct method of fm generation armstrong methodLearn By Watch
 
Direct method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodDirect method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodLearn By Watch
 
Spectrum and power of wbfm
Spectrum and power of wbfmSpectrum and power of wbfm
Spectrum and power of wbfmLearn By Watch
 
Narrow band frequency modulation nbfm
Narrow band frequency modulation nbfmNarrow band frequency modulation nbfm
Narrow band frequency modulation nbfmLearn By Watch
 
General expression of fm signal
General expression of fm signalGeneral expression of fm signal
General expression of fm signalLearn By Watch
 
Frequency division multiplexing
Frequency division multiplexingFrequency division multiplexing
Frequency division multiplexingLearn By Watch
 
Demodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorDemodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorLearn By Watch
 
Generarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodGenerarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodLearn By Watch
 
Generarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodGenerarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodLearn By Watch
 
Demodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverDemodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverLearn By Watch
 
Quadrature carrier multiplexing qam
Quadrature carrier multiplexing qamQuadrature carrier multiplexing qam
Quadrature carrier multiplexing qamLearn By Watch
 
Demodulation of am synchronous detector
Demodulation of am synchronous detectorDemodulation of am synchronous detector
Demodulation of am synchronous detectorLearn By Watch
 

More from Learn By Watch (20)

Tutorial 9 fm
Tutorial 9 fmTutorial 9 fm
Tutorial 9 fm
 
Phase modulation
Phase modulationPhase modulation
Phase modulation
 
Demodulation of fm pll detector
Demodulation of fm pll detectorDemodulation of fm pll detector
Demodulation of fm pll detector
 
Demodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detectorDemodulation of fm slope and balanced slope detector
Demodulation of fm slope and balanced slope detector
 
In direct method of fm generation armstrong method
In direct method of fm generation armstrong methodIn direct method of fm generation armstrong method
In direct method of fm generation armstrong method
 
Direct method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator methodDirect method of fm generation hartley oscillator method
Direct method of fm generation hartley oscillator method
 
Carson's rule
Carson's ruleCarson's rule
Carson's rule
 
Spectrum and power of wbfm
Spectrum and power of wbfmSpectrum and power of wbfm
Spectrum and power of wbfm
 
Narrow band frequency modulation nbfm
Narrow band frequency modulation nbfmNarrow band frequency modulation nbfm
Narrow band frequency modulation nbfm
 
General expression of fm signal
General expression of fm signalGeneral expression of fm signal
General expression of fm signal
 
Angle modulation
Angle modulationAngle modulation
Angle modulation
 
Frequency division multiplexing
Frequency division multiplexingFrequency division multiplexing
Frequency division multiplexing
 
Vsb modulation
Vsb modulationVsb modulation
Vsb modulation
 
Demodulation of ssb synchronous detector
Demodulation of ssb synchronous detectorDemodulation of ssb synchronous detector
Demodulation of ssb synchronous detector
 
Generarion of ssb phase discrimination method
Generarion of ssb phase discrimination methodGenerarion of ssb phase discrimination method
Generarion of ssb phase discrimination method
 
Generarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination methodGenerarion of ssb frequency discrimination method
Generarion of ssb frequency discrimination method
 
Ssb modulation
Ssb modulationSsb modulation
Ssb modulation
 
Demodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiverDemodulation of dsb sc costas receiver
Demodulation of dsb sc costas receiver
 
Quadrature carrier multiplexing qam
Quadrature carrier multiplexing qamQuadrature carrier multiplexing qam
Quadrature carrier multiplexing qam
 
Demodulation of am synchronous detector
Demodulation of am synchronous detectorDemodulation of am synchronous detector
Demodulation of am synchronous detector
 

File io

  • 1. C Programming Language By: Yogendra Pal yogendra@learnbywatch.com Dedicated to My mother and Father
  • 2. t y Keep your notebook with you. Write important point and questions that comes in your mind Solve Mind band exercise. C Rewind when not clear Ask Questions by call or SMS or by mail Keep Watching Keep Learning THIS IS FILE I/O 2
  • 3. File… • Files are used to store data to disks. • Data on disk is permanent. • Variables are stored on RAM only. • Data on RAM can not be accessed later. 3
  • 4. File… • File : Group of related records. Sonu 001 04/02 Amit 002 09/05 File Asif 003 10/09 Hari 004 30/11 Sonu 001 04/02 Record Sonu Field 1001001 byte 1 bit 4
  • 5. Files in C • A file is a sequence of bytes. – File ends with End Of File (EOF) marker. • Streams are used to communicate with file. stream 5
  • 6. File • Use FILE pointer to access a file. FILE *fp; • fopen() library function is used to open a file. – fopen( file_path, open_mode); – Returns NULL if file can not open. • fclose() library function is used to close a file. – fclose(file_pointer); 6
  • 7. File open mode Mode Description r Open a file for reading. w Create a file for writing. If the file already exists, discard the current contents. a Append; open or create a file for writing at end of file. r+ Open a file for update (reading and writing). w+ Create a file for update. If the file already exists, discard the current contents. a+ Append; open or create a file for update; writing is done at the end of the file. rb Open a file for reading in binary mode. wb Create a file for writing in binary mode. If the file already exists, discard the current contents. ab Append; open or create a file for writing at end of file in binary mode. rb+ Open a file for update (reading and writing) in binary mode. wb+ Create a file for update in binary mode. If the file already exists, discard the current contents. ab+ Append; open or create a file for update in binary mode; writing is done at the end of the file. 7
  • 8. Read / Write a file • fgetc() : reads one character from a file. • fputc() : write one character to a file. • fgets() : read a line (Srring) from a file. • fputs() : Write a line to a file. • fprintf() : used to print to a file. • fscanf() : used to read from a file. • feof() : return true if EOF find. • rewind() : set file position pointer to the start. 8
  • 9. Problem • Write a program to read a file and print it on monitor. • Write a program to create a copy of a file. • Modify first problem: stop printing if a ‘z’ occurs in file. • Write a program to create a file. 9
  • 10. Sequential Access File • Write or Read data sequentially. • Data can not modify without destroying. • Fields can vary in size. fopen(“one.dat”, “w”); 10
  • 11. Random Access File • Access any record without searching through other records. • Data can be inserted without destroying other data. • Implemented using fixed length records. fopen(“one.dat”, “wb”); • Also known as binary file. 11
  • 12. Random Access File… • fwrite() : used to write to binary file. • fread() : read from binary file. • fseek(): Used to set the file position pointer to a specific location. • Operation sequention. – Open/Create->seek->Write. – Open->seek->Read. 12
  • 13. fwrite() / fread() • fwrite(&data,sizeof(data),1,fp); • data : data that you want to write to file. • sizeof(data) : number of bytes to write. • 1 : for arrays, number of elements to write. • fp : file to write. – fread(&data,sizeof(data),1,fp); • &data : variable to store data. • sizeof(data) : number of bytes to read. • 1 : for arrays, number of elements to read. • fp : file from read. 13
  • 14. fseek() • fseek(fp,offset,symbolic_constant); – fp : File pointer. – offset : location of file position pointer. – symbolic_constant : specify where in file we are reading from. • SEEK_SET: beginning of file. • SEEK_CUR: current location in file. • SEEK_END: end of file. 14
  • 15. To get complete benefit of this tutorial solve all the quiz on www.learnbywatch.com For any problem in this tutorial mail me at yogendra@learnbywatch.com with the subject “C” For Other information mail at info@learnbywatch.com Keep Watching Keep Learning NOW YOU ARE READY TO LEARN C++ AND JAVA