Docsity

File Handling in C: Reading, Writing, and Stream Operations, Schemes and Mind Maps of Computer Science

Toggle sidebar
bg1
Lecture # 11
File Handling
Reading,fetching,getting,extracting data
Data โ†’ File (set of related records)
Stream โ†’ logical interface of file โ†’ flow of data from source to d
destination writing,storing,inserting, putting data.
โ– Associated (open operation)
โ– Not associated (close operation)
Stream Types
โ– Text Stream (sequence of characters) โ†’character
translationโ†’e.g. new line is represented as carriage return
โ– Binary Stream (sequence of bytes) โ†’ the number of bytes
written or read is always the same as those on external
devices/no character translation
File Markers
โ– EOF โ†’ start of first byte or character of file
โ– BOF โ†’ end of last byte or character of file
โ– Newline โ†’ ordinary file (Enter) โ†’ in C \n is used at the
end of each line
File opening
โ€ข Before read/write, file must be opened
โ€ข File handling functions โ†’ stdio.h
โ€ข File opened โ†’ portion of memory is reserved โ†’ memory
buffer
pf3
pf4
pf5

Partial preview of the text

Download File Handling in C: Reading, Writing, and Stream Operations and more Schemes and Mind Maps Computer Science in PDF only on Docsity!

Lecture # 1 1 File Handling Reading,fetching,getting,extracting data Data โ†’ File (set of related records) Stream โ†’ logical interface of file โ†’ flow of data from source to d destination writing,storing,inserting, putting data. โ– Associated (open operation) โ– Not associated (close operation) Stream Types โ– Text Stream (sequence of characters) โ†’character translationโ†’e.g. new line is represented as carriage return โ– Binary Stream (sequence of bytes) โ†’ the number of bytes written or read is always the same as those on external devices/no character translation File Markers โ– EOF โ†’ start of first byte or character of file โ– BOF โ†’ end of last byte or character of file โ– Newline โ†’ ordinary file (Enter) โ†’ in C \n is used at the end of each line File opening

  • Before read/write, file must be opened
  • File handling functions โ†’ stdio.h
  • File opened โ†’ portion of memory is reserved โ†’ memory buffer
  • Read/write โ†’ memory buffer (read/write through disk is time consuming so time reduced through buffer File Pointers
  • Pointer (*) is a variable that contains the address of another variable
  • File not accessed directly โ†’ but accessed through buffers
  • Pointer variable file type โ†’ FILE *fp;
  • Fopen() is used to open file โ†’ two parameters (Name of file, opening mode) Name of file: absolute path using โ†’ \ or \ Opening mode: string (enclosed in double quotes) โ†’ operation performed on file FILE *fpr; Fpr=fopen(โ€œc:\tc\XYZ.DATโ€, โ€œrโ€); File Opening modes โ– r โ†’ open text file for reading โ– w โ†’ open text file for writing (if file exists it is overwritten โ– a โ†’ open text file for append (Data append at the end of file) โ– r+ โ†’ open file for both reading and writing โ– w+ โ†’ open file for both reading and writing (contents overwritten) โ– a+ โ†’ open file for both reading and appending fopen() function returns null โ†’ fails to open file โ†’ file not exist

โ– Getc() โ†’ reads the next character from file โ– Returns EOF โ†’ if error occurs โ– To use this function file must be in open mode โ– Variable=getc(file_pointer); โ†’ variable is a character variable String Handling โ– Combination of characters enclosed in double quotes โ– Char (data type) name_of_string [string_length(no of characters)]; โ– Also declared without specifying number of characters โ– String variable โ†’ variable used to store string โ– Last character of string โ†’ null character \0 (it is included in string) โ– Char name[20]=โ€STEPโ€; โ– Char str[ ]= โ€œ I Love PGCโ€; โ– Header file โ†’ string.h โ– String functions โ†’ fputs(), fgets(), strcpy() etc Fputs()

  • Write string to file until null character \ 0 reached.
  • File must be opened or in append mode to use this function
  • Fputs(string, file_pointer); Example FILE *fp; Fp=fopen(โ€œtest.txtโ€,โ€wโ€); Fputs(โ€œString of your choiceโ€, fp);

Fgets()

  • Read string of characters of certain length
  • File must be opened or in append mode
  • Fgets(string,nโ†’ no.of characters, file_pointer) FILE *sr; Char str[80],fn[12]; Int c=0; Printf(โ€œEnter name of file to read dataโ€); Scanf(โ€œ%sโ€,&fn); Sr=fopen(fn, โ€œrโ€); If(sr==NULL) printf(โ€œFile doesnot existโ€); While(fgetsType equation here.