\documentclass{beamer}
%
%
\makeatletter
\@ifundefined{lhs2tex.lhs2tex.sty.read}%
  {\@namedef{lhs2tex.lhs2tex.sty.read}{}%
   \newcommand\SkipToFmtEnd{}%
   \newcommand\EndFmtInput{}%
   \long\def\SkipToFmtEnd#1\EndFmtInput{}%
  }\SkipToFmtEnd

\newcommand\ReadOnlyOnce[1]{\@ifundefined{#1}{\@namedef{#1}{}}\SkipToFmtEnd}
\usepackage{amstext}
\usepackage{amssymb}
\usepackage{stmaryrd}
\DeclareFontFamily{OT1}{cmtex}{}
\DeclareFontShape{OT1}{cmtex}{m}{n}
  {<5><6><7><8>cmtex8
   <9>cmtex9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmtex10}{}
\DeclareFontShape{OT1}{cmtex}{m}{it}
  {<-> ssub * cmtt/m/it}{}
\newcommand{\texfamily}{\fontfamily{cmtex}\selectfont}
\DeclareFontShape{OT1}{cmtt}{bx}{n}
  {<5><6><7><8>cmtt8
   <9>cmbtt9
   <10><10.95><12><14.4><17.28><20.74><24.88>cmbtt10}{}
\DeclareFontShape{OT1}{cmtex}{bx}{n}
  {<-> ssub * cmtt/bx/n}{}
\newcommand{\tex}[1]{\text{\texfamily#1}}	% NEU

\newcommand{\Sp}{\hskip.33334em\relax}
\newcommand{\NB}{\textbf{NB}}
\newcommand{\Todo}[1]{$\langle$\textbf{To do:}~#1$\rangle$}

\EndFmtInput
\makeatother
%
\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 \text{\tt FILE} type}
  \begin{itemize}
    \pause
  \item A file is represented using a pointer to a \text{\tt FILE} structure.
    \pause
  \item All operations on a file take this pointer as argument.
    \pause
  \item The exact fields of the \text{\tt FILE} structure are not relevant to
    us. It is \text{\tt FILE~\char42{}} that is interesting form the C programmers
    perspective.
  \end{itemize}
\end{frame}
\begin{frame}
  \frametitle{Opening a file}
  \begin{tabbing}\tt
~~~~~FILE~\char42{}~fopen\char40{}char~\char42{}filename\char44{}~char~\char42{}~mode\char41{}\char59{}
\end{tabbing}
  \begin{itemize}
  \item The function returns a \text{\tt FILE~\char42{}}.
  \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 \text{\tt 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 \text{\tt man~fopen}
  \end{itemize}
\end{frame}
\begin{frame}
  \begin{block}{Standard idiom of opening files}
\begin{tabbing}\tt
~FILE~\char42{}fp\char59{}\\
\tt ~if\char40{}~\char40{}fp~\char61{}~fopen\char40{}\char34{}foo\char47{}bar\char47{}biz\char34{}\char44{}~\char34{}r\char34{}\char41{}~\char61{}\char61{}~NULL~\char41{}\\
\tt ~\char123{}\\
\tt ~~~\char47{}\char42{}~File~does~not~exists~or~some~error~has~occured\char46{}~\\
\tt ~~~Handle~it\char42{}\char47{}\\
\tt ~\char125{}else~\char123{}\\
\tt ~~~\char47{}\char42{}~do~some~some~thing~useful~with~the~file~\char42{}\char47{}\\
\tt ~\char125{}
\end{tabbing}
  \end{block}
\end{frame}
\begin{frame}
  As far as \text{\tt C} is concerned, a file is a sequence of
  characters.
  \pause
  \begin{block}{The functions \text{\tt fgetc} and \text{\tt fputc}}
    \begin{tabbing}\tt
~~~~~~~int~fgetc\char40{}FILE~\char42{}infp\char41{}\char59{}
\end{tabbing}
  \end{block}
  \begin{itemize}
  \item \text{\tt fgetc} reads a character for the the file \text{\tt infp}.
    \pause
  \item \text{\tt infp} should have been opened in read mode.
    \pause
  \item The value returned by \text{\tt fgetc} is \text{\tt EOF} if end of file is reached.
  \end{itemize}
  \pause
  \begin{tabbing}\tt
~~~~~int~fputc\char40{}int~c\char44{}~file~\char42{}outfp\char41{}\char59{}
\end{tabbing}
  \pause
  \begin{itemize}
  \item \text{\tt fputc} writes the character corresponding to \text{\tt c} in \text{\tt outfp}.
    \pause
  \item \text{\tt outfp} should have been opened in write or append mode.
  \end{itemize}
\end{frame}
\begin{frame}
  \frametitle{Standard idiom to use \text{\tt fgetc} and \text{\tt fputc}}
  \begin{tabbing}\tt
~~~void~copy\char40{}FILE~\char42{}infp\char44{}~FILE~\char42{}outfp\char41{}\\
\tt ~~~\char123{}\\
\tt ~~~~~int~c\char59{}\\
\tt ~~~~~while\char40{}~\char40{}c~\char61{}~fgetc\char40{}infp\char41{}\char41{}~\char33{}\char61{}~EOF\char41{}~fputc\char40{}c\char44{}~outfp\char41{}\char59{}\\
\tt ~~~\char125{}
\end{tabbing}
\end{frame}

\begin{frame}
\frametitle{The function \text{\tt fscanf} and \text{\tt fprintf}}

\begin{tabbing}\tt
~~~int~fscanf\char40{}FILE~\char42{}infp\char44{}~char~\char42{}fmt\char44{}\char46{}\char46{}\char46{}\char41{}\char59{}\\
\tt ~~~int~fprintf\char40{}FILE~\char42{}outfp\char44{}~char~\char42{}fmt\char44{}~\char46{}\char46{}\char46{}\char41{}\char59{}
\end{tabbing}
Same as \text{\tt scanf} and \text{\tt printf} but uses files

\end{frame}

\begin{frame}
  \frametitle{Files opened at the start of the program} 
  
  \begin{itemize}
  \item  The files \text{\tt FILE~\char42{}stdin\char44{}~stdout\char44{}~stderr} are open when the program
    starts.
  \item As the name suggests \text{\tt stdin} is the input, \text{\tt stdout} is the output
    and \text{\tt stderr} is for sending error messages.
  \end{itemize}
  
  eg. \text{\tt printf\char40{}\char34{}\char37{}d~\char37{}c\char34{}\char44{}x\char44{}y\char41{}} is equivalent to \text{\tt fprintf\char40{}stdout\char44{}\char34{}\char37{}d~\char37{}c\char34{}\char44{}x\char44{}y\char41{}}

  \begin{block}{Why do we need \text{\tt stderr}}
  \end{block}
  
\end{frame}

\begin{frame}
  \frametitle{\text{\tt cat} revisited}
\input{cat}
\end{frame}
\end{document}
