\documentclass{beamer} %include lhs2TeX.fmt \author{Piyush P Kurur\\ Office no: 224\\ Dept. of Comp. Sci. and Engg.\\ IIT Kanpur} \newcommand{\Token}[2][]{\node(#2#1)[token]{#2};} \newcommand{\Symbol}[2][]{\node(#2#1)[symbol]{#2};} \newcommand{\Point}[1]{\node(#1)[point]{#1};} \usepackage{tikz} \usetikzlibrary{positioning,shapes,chains,fit} \usetikzlibrary{shapes.symbols} \usetikzlibrary{matrix} \usetikzlibrary{backgrounds} \usetikzlibrary{shapes.geometric} \usepackage{multicol} \usepackage{algorithm2e} \usepackage{pgfkeys} %% \newcommand{\SingleLinkedList}[1]{ %% \foreach \datum/\anchor in {#1} %% \begin{scope} %% \node[rectangle, fill=yellow!30]{#2}; %% \node[rectangle, below, fill=green!30](#1){$\cdot$}; %% \end{scope} %% } %% \newcommand{\Null}{ %% \node[circle, fill=black, minimum size=1em]{}; %% } \pgfkeys {% /tikz/redbox/.style={% shape=rectangle,% minimum size=1.5em,% top color = white, % bottom color=red!50!black!50, inner sep=0pt, draw=black }, /tikz/null/.style={% shape=circle, minimum size=0.5em, draw=black, }, /tikz/greybox/.style={ shape=rectangle,% minimum width=7em,% minimum height=1.5em, fill=black!20, inner sep=0pt, draw=black }, /tikz/blackbox/.style={ shape=rectangle,% minimum width=7em,% minimum height=1.5em, fill=black, inner sep=0pt, draw=black }, /tikz/whitebox/.style={ shape=rectangle, minimum width=7em, minimum height=1.5em, draw=black }, /tikz/bluebox/.style={ shape=rectangle, minimum width=7em, minimum height=1.5em, fill=blue!40, draw=black }, /tikz/whitetape/.style={ shape=rectangle, minimum width=4em, minimum height=1.5em, draw=black }, /tikz/bluearr/.style={ ->, line width=0.25em, draw=blue!40 }, /tikz/greenarr/.style={ ->, line width=0.25em, draw=green!40!black!60 } } \newcommand{\arraythree}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1, right=of a0#3](a1#3){#2}; \node[#1, right=of a1#3](a1#3){#2}; \end{tikzpicture} } \newcommand{\arraytwo}[4]{ \begin{tikzpicture}[node distance=0] \pgfsetstrokeopacity{#4}; \node[#1](a0#3){#2}; \node[#1,right=of a0#3]{#2}; \end{tikzpicture} } \newcommand{\Array}[0]{ \arraythree{box}{}{a}{1} } \newcommand{\AArray}[0]{% \arraythree{bbox}{\Array}{b}{0.5}} \newcommand{\AAArray}[0]{% \arraytwo{bbbox}{\AArray}{c}{0.25}} \newcommand{\T}[1]{\ensuremath{T\left(#1\right)}} \newcommand{\BigOh}[1]{\ensuremath{O\left(#1\right)}} \title{Fundamentals of Computing: Lecture 31} \date{October 21, 2009} \begin{document} \begin{frame} \maketitle \end{frame} \begin{frame} \frametitle{Summary of last class} \begin{itemize} \pause \item Files and directories \pause \begin{itemize} \item Files are collection of data \item In Unix almost every thing is a filed \item Files are organised into directories. \end{itemize} \pause \item Operations on a file (open, read/write, close) \end{itemize} \end{frame} \begin{frame} \input{cat} \end{frame} \begin{frame} \frametitle{The |FILE| type} \begin{itemize} \pause \item A file is represented using a pointer to a |FILE| structure. \pause \item All operations on a file take this pointer as argument. \pause \item The exact fields of the |FILE| structure are not relevant to us. It is |FILE *| that is interesting form the C programmers perspective. \end{itemize} \end{frame} \begin{frame} \frametitle{Opening a file} \begin{spec} FILE * fopen(char *filename, char * mode); \end{spec} \begin{itemize} \item The function returns a |FILE *|. \item On error returns a null pointer. \item The mode parameter has the following interpretation \begin{itemize} \item "r" means read. If the file does not exists |fopen| returns NULL. \item "w" means write. If the file exists then truncates it. \item "a" write at the end of the file. The contents are kept, and file created if it does not exist. \end{itemize} \item You can also give "rw" for read and write. \item For more details type |man fopen| \end{itemize} \end{frame} \begin{frame} \begin{block}{Standard idiom of opening files} \begin{spec} FILE *fp; if( (fp = fopen("foo/bar/biz", "r") == NULL ) { /* File does not exists or some error has occured. Handle it*/ }else { /* do some some thing useful with the file */ } \end{spec} \end{block} \end{frame} \begin{frame} As far as |C| is concerned, a file is a sequence of characters. \pause \begin{block}{The functions |fgetc| and |fputc|} \begin{spec} int fgetc(FILE *infp); \end{spec} \end{block} \begin{itemize} \item |fgetc| reads a character for the the file |infp|. \pause \item |infp| should have been opened in read mode. \pause \item The value returned by |fgetc| is |EOF| if end of file is reached. \end{itemize} \pause \begin{spec} int fputc(int c, file *outfp); \end{spec} \pause \begin{itemize} \item |fputc| writes the character corresponding to |c| in |outfp|. \pause \item |outfp| should have been opened in write or append mode. \end{itemize} \end{frame} \begin{frame} \frametitle{Standard idiom to use |fgetc| and |fputc|} \begin{spec} void copy(FILE *infp, FILE *outfp) { int c; while( (c = fgetc(infp)) != EOF) fputc(c, outfp); } \end{spec} \end{frame} \begin{frame} \frametitle{The function |fscanf| and |fprintf|} \begin{spec} int fscanf(FILE *infp, char *fmt,...); int fprintf(FILE *outfp, char *fmt, ...); \end{spec} Same as |scanf| and |printf| but uses files \end{frame} \begin{frame} \frametitle{Files opened at the start of the program} \begin{itemize} \item The files |FILE *stdin, stdout, stderr| are open when the program starts. \item As the name suggests |stdin| is the input, |stdout| is the output and |stderr| is for sending error messages. \end{itemize} eg. |printf("%d %c",x,y)| is equivalent to |fprintf(stdout,"%d %c",x,y)| \begin{block}{Why do we need |stderr|} \end{block} \end{frame} \begin{frame} \frametitle{|cat| revisited} \input{cat} \end{frame} \end{document}