From 92cca121f959c6616e3da431c1e2d23c4fa5e886 Mon Sep 17 00:00:00 2001 From: hardythe1 Date: Tue, 7 Apr 2015 15:58:05 +0530 Subject: added books --- .../Chapter11.ipynb | 616 +++++++++++++++++++++ 1 file changed, 616 insertions(+) create mode 100755 C_Programming_for_the_Absolute_Beginner/Chapter11.ipynb (limited to 'C_Programming_for_the_Absolute_Beginner/Chapter11.ipynb') diff --git a/C_Programming_for_the_Absolute_Beginner/Chapter11.ipynb b/C_Programming_for_the_Absolute_Beginner/Chapter11.ipynb new file mode 100755 index 00000000..239b6ca4 --- /dev/null +++ b/C_Programming_for_the_Absolute_Beginner/Chapter11.ipynb @@ -0,0 +1,616 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:9d02541e3ea8d7af1efd977534b7150b993557acd6da8dc30b83dcacb798d681" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "heading", + "level": 1, + "metadata": {}, + "source": [ + "Chapter 11 : File Input and Output" + ] + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.1, Page No 250" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "try:\n", + " pRead = open(\"file1.dat\",\"r\")\n", + "except IOError:\n", + " print \"\\nFile can not be opened\\n\"\n", + "\n", + "\"\"\"\n", + "Input file:- file1.dat\n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.2, Page No 252" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "try:\n", + " pRead = open(\"file1.dat\",\"r\")\n", + " if(not isinstance(pRead,file)):\n", + " print \"\\nFile can not be opened\\n\"\n", + " else:\n", + " print \"\\nFile opened for reading\\n\"\n", + " pRead.close()\n", + "except IOError:\n", + " print \"\\nFile can not be opened\\n\"\n", + "\n", + "\"\"\"\n", + "Input file:- file1.dat\n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "File opened for reading\n", + "\n" + ] + } + ], + "prompt_number": 2 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.3, Page No 253" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "try:\n", + " pRead = open(\"names.dat\",\"r\")\n", + " if(not isinstance(pRead,file)):\n", + " print \"\\nFile can not be opened\\n\"\n", + " else:\n", + " print \"\\nContents of names.dat\\n\"\n", + " while(1):\n", + " t = pRead.readline() \n", + " if(t != \"\"):\n", + " print t\n", + " else:\n", + " break\n", + " pRead.close()\n", + "except IOError:\n", + " print \"\\nFile can not be opened\\n\"\n", + " \n", + "\"\"\" \n", + "Input File :- names.dat\n", + "\n", + "Michael\n", + "Sheila\n", + "Spencer\n", + "Olivia\n", + "\n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Contents of names.dat\n", + "\n", + "Michael\n", + "\n", + "Sheila\n", + "\n", + "Spencer\n", + "\n", + "Olivia\n" + ] + } + ], + "prompt_number": 3 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.4, Page No 255" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "try:\n", + " pRead = open(\"hobbies.dat\",\"r\")\n", + " if(not isinstance(pRead,file)):\n", + " print \"\\nFile can not be opened\\n\"\n", + " else:\n", + " print \"\\nName\\tHobby\\n\\n\"\n", + " while(1):\n", + " name = pRead.readline()\n", + " hobby = pRead.readline()\n", + " if(name != \"\"):\n", + " print name + \"\\t\"\n", + " print hobby\n", + " else:\n", + " break\n", + " pRead.close()\n", + "except IOError:\n", + " print \"\\nFile can not be opened\\n\"\n", + " \n", + "\"\"\"\n", + "Input file :- hobbies.dat\n", + "\n", + "Michael\tProgramming\n", + "Sheila\tShopping\n", + "Spencer\tFootball\n", + "Olivia\tDancing\n", + "Waytt\tEating\n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Name\tHobby\n", + "\n", + "\n", + "Michael\tProgramming\n", + "\t\n", + "Sheila\tShopping\n", + "\n", + "Spencer\tFootball\n", + "\t\n", + "Olivia\tDancing\n" + ] + } + ], + "prompt_number": 5 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.5, Page No 256" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "try:\n", + " pWrite = open(\"students.dat\",\"w\")\n", + " if(not isinstance(pWrite,file)):\n", + " print \"\\nFile can not be opened\\n\"\n", + " else:\n", + " fname = raw_input(\"Enter first name: \")\n", + " lname = raw_input(\"Enter last name: \")\n", + " id1 = raw_input(\"Enter id: \")\n", + " gpa = float(raw_input(\"Enter GPA: \"))\n", + " pWrite.writelines(fname + \" \" + lname + \"\\t\" + id1 + \"\\t\" + str(gpa))\n", + " pWrite.close()\n", + "except IOError:\n", + " print \"\\nFile can not be opened\\n\"\n", + "\n", + "\"\"\"\n", + "Output file:- students.dat\n", + "Patric Star\t888-66-9999\t2.0\n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter first name: Patric\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter last name: Star\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter id: 888-66-9999\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter GPA: 2.00\n" + ] + } + ], + "prompt_number": 7 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.6, Page No 258" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "try:\n", + " pRead = open(\"students.dat\",\"r\")\n", + " if(not isinstance(pRead,file)):\n", + " print \"\\nFile can not be opened\\n\"\n", + " else:\n", + " print \"\\nName\\t\\tID\\t\\tGPA\\n\\n\"\n", + " while(1):\n", + " fname = pRead.read()\n", + " lname = pRead.read()\n", + " id1 = pRead.read()\n", + " gpa = pRead.read()\n", + " \n", + " if(fname != \"\"):\n", + " print fname + lname + \"\\t\\t\" + id1 + \"\\t\" + str(gpa)\n", + " else:\n", + " break\n", + " pRead.close()\n", + "except IOError:\n", + " print \"\\nFile can not be opened\\n\"\n", + "\n", + "\"\"\"\n", + "Input file:- students.dat\n", + "Patric Star\t888-66-9999\t2.0\n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Name\t\tID\t\tGPA\n", + "\n", + "\n", + "Patric Star\t888-66-9999\t2.0\t\t\t\n" + ] + } + ], + "prompt_number": 8 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.7, Page No 259" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "def readData():\n", + " try:\n", + " pRead = open(\"hobbies.dat\",\"r\")\n", + " if(not isinstance(pRead,file)):\n", + " print \"\\nFile can not be opened\\n\"\n", + " else:\n", + " print \"\\nName\\tHobby\\n\\n\"\n", + " while(1):\n", + " name = pRead.readline()\n", + " hobby = pRead.readline()\n", + " if(name != \"\"):\n", + " print name + \"\\t\"\n", + " print hobby\n", + " else:\n", + " break\n", + " pRead.close()\n", + " except IOError:\n", + " print \"\\nFile can not be opened\\n\"\n", + "try:\n", + " print \"Current file contains\"\n", + " readData()\n", + " print \"\\n\"\n", + " name = raw_input(\"Enter a new name: \")\n", + " hobby = raw_input(\"Enter a new hobby: \")\n", + " pWrite = open(\"hobbies.dat\",\"a\")\n", + " if(not isinstance(pWrite,file)):\n", + " print \"\\nFile can not be opened\\n\"\n", + " else:\n", + " pWrite.writelines(\"\\n\" + name + \"\\t\" + hobby)\n", + " pWrite.close()\n", + " readData()\n", + "except IOError:\n", + " print \"\\nFile can not be opened\\n\"\n", + " \n", + "\"\"\"\n", + "Input file and Output file:- hobbies.dat\n", + "Michael\tProgramming\n", + "Sheila\tShopping\n", + "Spencer\tFootball\n", + "Olivia\tDancing\n", + "Waytt\tEating\n", + "\"\"\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "Current file contains\n", + "\n", + "Name\tHobby\n", + "\n", + "\n", + "Michael\tProgramming\n", + "\t\n", + "Sheila\tShopping\n", + "\n", + "Spencer\tFootball\n", + "\t\n", + "Olivia\tDancing\n", + "\n", + "\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a new name: Waytt\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Enter a new hobby: Eating\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Name\tHobby\n", + "\n", + "\n", + "Michael\tProgramming\n", + "\t\n", + "Sheila\tShopping\n", + "\n", + "Spencer\tFootball\n", + "\t\n", + "Olivia\tDancing\n", + "\n", + "Waytt\tEating\t\n", + "\n" + ] + } + ], + "prompt_number": 16 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.8, Page No 263" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import sys\n", + "try:\n", + " pRead = open(\"hobbies.dat\",\"r\")\n", + " if(not isinstance(pRead,file)):\n", + " print \"\\nFile can not be opened\\n\"\n", + " print \"The following error occurred\" #goto label\n", + " sys.exit(\"Some Error Occures\")\n", + " else:\n", + " print \"\\nName\\tHobby\\n\\n\"\n", + " while(1):\n", + " name = pRead.readline()\n", + " hobby = pRead.readline()\n", + " if(name != \"\"):\n", + " print name + \"\\t\"\n", + " print hobby\n", + " else:\n", + " break\n", + " pRead.close()\n", + " #sys.exit(\"Exit\")\n", + "except IOError:\n", + " print \"\\nFile can not be opened\\n\"\n", + "\n", + "\"\"\"\n", + "Input file:- hobbies.dat\n", + "Michael\tProgramming\n", + "Sheila\tShopping\n", + "Spencer\tFootball\n", + "Olivia\tDancing\n", + "Waytt\tEating\n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "Name\tHobby\n", + "\n", + "\n", + "Michael\tProgramming\n", + "\t\n", + "Sheila\tShopping\n", + "\n", + "Spencer\tFootball\n", + "\t\n", + "Olivia\tDancing\n", + "\n", + "Waytt\tEating\t\n", + "\n" + ] + } + ], + "prompt_number": 20 + }, + { + "cell_type": "heading", + "level": 2, + "metadata": {}, + "source": [ + "Example 11.9, Page No 266" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "print \"\\n\\tPhone Book\\n\"\n", + "print \"\\n 1 \\t Add phone book entry \\n\"\n", + "print \"2 \\t Print phone book \\n\\n\"\n", + "print \"Select an option: \"\n", + "response = int(raw_input(\"Select an option: \"))\n", + "print response\n", + "if(response == 1):\n", + " fname = raw_input(\"Enter first name: \")\n", + " lname = raw_input(\"Enter last name: \")\n", + " number = raw_input(\"Enter phone number: \")\n", + " pWrite = open(\"phone_book.dat\",\"a\")\n", + " if(isinstance(pWrite,file)):\n", + " pWrite.writelines(fname + \" \" + lname + \" \" + number + \"\\n\")\n", + " pWrite.close()\n", + " else:\n", + " print \"The error occured\" #go to\n", + "elif(response == 2):\n", + " pRead = open(\"phone_book.dat\",\"r\")\n", + " if(isinstance(pRead,file)):\n", + " while(1):\n", + " fname = pRead.read()\n", + " if(fname == \"\"):\n", + " break\n", + " lname = pRead.read()\n", + " number = pRead.read()\n", + " print fname\n", + " print lname\n", + " print number\n", + " print \"\\n\"\n", + " else:\n", + " print \"The error occured\" #go to\n", + "else:\n", + " print \"Invalid Selection\"\n", + "\n", + "\"\"\"\n", + "Input file and Output file:- phone_book.dat\n", + "John Smith 538.676.1234\n", + "\"\"\"" + ], + "language": "python", + "metadata": {}, + "outputs": [ + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "\n", + "\tPhone Book\n", + "\n", + "\n", + " 1 \t Add phone book entry \n", + "\n", + "2 \t Print phone book \n", + "\n", + "\n", + "Select an option: \n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "stream": "stdout", + "text": [ + "Select an option: 2\n" + ] + }, + { + "output_type": "stream", + "stream": "stdout", + "text": [ + "2\n", + "John Smith 538.676.1234\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + } + ], + "prompt_number": 1 + } + ], + "metadata": {} + } + ] +} \ No newline at end of file -- cgit