Você está na página 1de 30

File

Operations &
System
Services

Introduction
Objective

To introduce:

basic methods to
manipulate text files in
C programs using
sequential and random
access techniques

Learning Outcome

At the end of this


chapter students are
expected to be able to

open, read and write


text files in C program
using sequential and
random access
techniques

using appropriate
functions for dynamic
memory allocation

dynamic memory
allocation technique

Overview

File Operation & System


Services

File
manipulation

Sequential
access

Random
access

Dynamic
memory
allocation

File

Why?

Data in variables & arrays are stored in


temporary/volatile memory (RAM)

when the program is terminated the data are lost

Files offer option for permanent data storage

the data are stored in secondary


devices/nonvolatile storage, ex.: harddisk

data can be obtained/read or stored/write from or


to files

File & Streams

In C programs, file streams are viewed as continuous


stream of bytes

Each file is ended with end of file marker or a specific


byte number

A stream is automatically associated with an open


file

It create communication channel between files and


programs

In computer OS (Unix, Windows), three file streams are


opened automatically.

stdin (Keyboard or redirected input file)

stdout (Screen or redirected output file)

stderr (Screen)

Redirection

OS allows computer file as I/O

Redirection key is used during execution

One input and one output file

Key
<

Input, to feed text file as input

>

Output, to dump output into text file

Syntax: executableFileName < input.txt > output.txt

Redirection
#include <stdio.h>

int main( ) {
printf("Hello World!");
return 0;
}

Redirection
#include <stdio.h>
int main( ) {

char str[100];
gets(str);
printf("%s",str);
return 0;

Redirection

Advantage: simple

Limitation: Only ONE file during runtime

File Operations in C

Allow access to multiple files

Modes

Sequential
access

Cassette

Random
access

CD

Some File Functions


Function

Description

fgetc

get character one by one


fgetc(stdin) getchar()

fputc

write single character


fputc(a,stdout) putchar(a)

fgets

read a line from file

fputs

write a line to file

fscanf

file processing function similar to scanf

fprintf

file processing function similar to printf

fread

used in random access

fwrite

Sequential Access

Sequential Access
File pointer advances from first character in the file
towards EOF marker
Sequential file functions:
Read (fgetc, fscanf, fgets)
Write (fputc, fprintf, fputs)

Advantage:
- Simpler code

Limitations:
- Data can be overwritten if not careful.
- Slower access speed

Sequential Access
Function
reading

fgetc (FILE *fPtr)

fgets (char *Str, int n, FILE


*fPtr)

Equivalent
Usage
standard
functions
getc
Reads and returns the next character from the stream
fPtr.
gets
Reads characters from stream fPtr into the string
pointed to by Str.
The integer argument n indicates the maximum
number of characters that the buffer Str can store.
Reading stops when a newline is read, end-of-file is
encountered, a read error occurs, or n-1 characters
were read.

writing

fscanf (FILE *fPtr, const char


*format, ...)

scanf

fputc (int c, FILE* fPtr)


fprintf (FILE *fPtr, const char
*format, arg0... argn)

putc
printf

fputs (const char *Str, FILE


*fPtr)

puts

feof(FILE *fptr)

New lines are included in the string. The string read is


terminated with a '\0.
Converts read characters according to the format
string and the values created are stored through the
argument pointers.
Writes the character c to the file stream fPtr.
Writes formatted data to the file stream fPtr.
Writes the string Str (excluding the terminating '\0') to
the stream fPtr.
to determine end of file, If the EOF marker is reached,
a non-zero value is returned, otherwise 0 is returned

This program:
1. creates files to write students ID,
first name, gpa and cgpa
2. read from the students files and
print the data in tabular form.
One file for one student.
The files are named based on student
ID

Opening file

Declare file pointer

Ex: FILE *fptr;

fopen is used to request external file access from


system

If file cant be access NULL value will be returned

File is opened based on opening modes

Opening Mode

:open existing file for reading

w
:create file for writing, if file already
existed discard the content

a
:add record to the end of existing file,
appending

r+ :open existing file for reading & writing

w+
:create file for reading & writing, if file
already existed the content is discarded

a+
:read & write from file, writing is done at
end of file

:binary mode

Opening Mode
Mode

Read

Write

Create

Discard

+
b

/
Binary mode

File pointer declaration


A string array to hold file name
File name according to id

Open & create file for


writing only

Open file for reading only


Checking file
accessibility

Creating & Writing


int sprintf(char *str,
const char *format, ...)
Sends formatted output
to a string.

Create file name


according to id
Open & create file for
writing only

Writing string of
character (name)
into file

Writing formatted
data into file

Creating & Writing

Reading
Open & create file for
reading only
Reading data
according to
format string

Reading

Closing File

If file is not closed properly, other programs might


not able to access the file

fclose(fptr);

Closing File

File is closed after


current students
data is written

File is closed after


file is read

Appending
Additional data of total
credit hour taken is to
be added in all
students files

Open file to append


and write data at end
of files

Write at end of file

Appending

Rewind

To re-read same file

rewind (fPtr);

Caused file position pointer to be repositioned to


beginning of file

Você também pode gostar