accessing files in c l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Accessing Files in C PowerPoint Presentation
Download Presentation
Accessing Files in C

Loading in 2 Seconds...

play fullscreen
1 / 14

Accessing Files in C - PowerPoint PPT Presentation


  • 165 Views
  • Uploaded on

Accessing Files in C. CS-2303 System Programming Concepts (Slides include materials from The C Programming Language , 2 nd edition, by Kernighan and Ritchie and from C: How to Program , 5 th and 6 th editions, by Deitel and Deitel). Two Kinds of File Access. Stream

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Accessing Files in C' - alyson


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
accessing files in c

Accessing Files in C

CS-2303System Programming Concepts

(Slides include materials from The C Programming Language, 2nd edition, by Kernighan and Ritchie and from C: How to Program, 5th and 6th editions, by Deitel and Deitel)

Accessing Files in C

two kinds of file access
Two Kinds of File Access
  • Stream
      • File is treated as a sequence of bytes
      • Access is sequential – i.e., in byte order
      • Cannot replace data in the middle of a file
  • Raw
      • File is a sequence of blocks
      • Any block can be read and/or written independent

Accessing Files in C

definition file
Definition – File

A (potentially) large amount of information or data that lives a (potentially) very long time

Often much larger than the memory of the computer

Often much longer than any computation

Sometimes longer than life of machine itself

(Usually) organized as a linear array of bytes or blocks

Internal structure is imposed by application

(Occasionally) blocks may be variable length

(Often) requiring concurrent access by multiple threads or processes

Even by processes on different machines!

Accessing Files in C

implementations of files
Implementations of Files

Usually on disks (or devices that mimic disks)

Magnetic – hard drive or floppy

Optical – CD, DVD

Flash drives – electronic memory, organized as disks

Requirement

Preserve data contents during power-off or disasters

Directory / Folder

Special kind of file that contains links pointing to other files

Associates names with files

Accessing Files in C

implementations of files5
Implementations of Files

Usually on disks (or devices that mimic disks)

Magnetic – hard drive or floppy

Optical – CD, DVD

Flash drives – electronic memory, organized as disks

Requirement

Preserve data contents during power-off or disasters

Directory / Folder

Special kind of file that contains links pointing to other files

Associates names with files

Older systems also used magnetic tape, paper tape, trays of punched cards, etc.

Accessing Files in C

organization of file systems
Organization of File Systems

Contiguous

Blocks stored contiguously on storage medium

E.g., CD, DVD, some large database systems

Access time to any block is O(1)

Linked

Blocks linked together – File Allocation Table (FAT)

Access time is O(n)

Indexed

Blocks accessed via tree of index blocks (i-nodes)

Access time is O(log n)

However, base of logarithm may be very large (>100)

Accessing Files in C

organization of file systems7
Organization of File Systems

Contiguous

Blocks stored contiguously on storage medium

E.g., CD, DVD, some large database systems

Access time to any block is O(1)

Linked

Blocks linked together – File Allocation Table (FAT)

Access time is O(n)

Indexed

Blocks accessed via tree of index blocks (i-nodes)

Access time is O(log n)

However, base of logarithm may be very large (>100)

NTFS and Linux file systems on hard drives

Accessing Files in C

organization of file systems8
Organization of File Systems

Contiguous

Blocks stored contiguously on storage medium

E.g., CD, DVD, some large database systems

Access time to any block is O(1)

Linked

Blocks linked together – File Allocation Table (FAT)

Access time is O(n)

Indexed

Blocks accessed via tree of index blocks (i-nodes)

Access time is O(log n)

However, base of logarithm may be very large (>100)

Typical camera chips, flash drives, floppies

Accessing Files in C

stream file access
Stream File Access

fgetc(), fgets(), fputs(), fputc(),fscanf(), fprintf(), ...fopen(), fclose()

  • Familar tools

fread(), fwrite(), fseek(), ftell(),rewind(), fgetpos(), fsetpos()

  • Not so familiar
  • Note:– if you seek to a position in a file and start writing, file is truncated at that point

All take FILE * argument to identify the file.

Declared in <stdio.h>

Accessing Files in C

raw file access
Raw File Access

See Kernighan & Ritchie, Chapter 8

Raw file access

Without simplifying stream functions – e.g.,

scanf, fscanf, printf, fprintf, fgetc, etc.

read and write raw disk blocks

Seek to a file position

lseek, fseek — sets file pointer to specified location

Subsequent read, write, etc., start there

ftell – returns file pointer

Accessing Files in C

raw file access continued
Raw File Access (continued)

#include <fcntl.h>

int fd;

int open(char *name, int flags, int perms);

int creat(char *name, int perms);

int read(fs, buf, n);

int write(fs, buf, n);

long lseek(fd, long offset, int origin);

Accessing Files in C

raw file access continued12
Raw File Access (continued)
  • Also functions for listing directories, adding things to directories, linking files, etc.
      • All are essentially calls to the OS
  • Consult man pages for details
      • man 3p open
      • man 3p read
      • man 3p write
      • ...

POSIX standard — Most likelyto be portable across platforms

Accessing Files in C

streams
Streams
  • Stream access is a layer of abstraction on top of raw access
      • See K&R §8.1–8.6 for example implementations

Accessing Files in C

questions

Questions?

Accessing Files in C