diff options
Diffstat (limited to '_Programming_With_C/chapter13.ipynb')
-rw-r--r-- | _Programming_With_C/chapter13.ipynb | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/_Programming_With_C/chapter13.ipynb b/_Programming_With_C/chapter13.ipynb new file mode 100644 index 00000000..fca3666f --- /dev/null +++ b/_Programming_With_C/chapter13.ipynb @@ -0,0 +1,183 @@ +{
+ "metadata": {
+ "name": ""
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h1>Chapter 13: File Handling<h1>"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.3, Page 12.4<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Writing to a data file\n",
+ "\n",
+ "fp=open(\"output.txt\",'w')\n",
+ "for k in range(65,91):\n",
+ "\tfp.write(str(k)+'\\n')\n",
+ "fp.close()\n",
+ "\n",
+ "\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 5
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.7, Page 13.8<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Creating a file containing customer records\n",
+ "\n",
+ "from ctypes import *\n",
+ "\n",
+ "string=c_char*40\n",
+ "\n",
+ "class date(Structure):\n",
+ "\t_fields_=[('month',c_int),('day',c_int),('year',c_int)]\n",
+ "\t\n",
+ "class account(Structure):\n",
+ "\t_fields_=[('name',string),('street',string),('city',string),('acct_no',c_int),('oldbalance',c_float),('payment',c_float),('lastpayment',date),('acct_type',c_char),('newbalance',c_float)]\n",
+ "\n",
+ "\n",
+ "def writefile(obj,fpt):\n",
+ "\tfpt.write(obj.name+'\\n')\n",
+ "\tfpt.write(obj.city+'\\n')\n",
+ "\tfpt.write(str(obj.acct_no)+'\\n')\n",
+ "\tfpt.write(obj.acct_type+'\\n')\n",
+ "\tfpt.write(str(obj.oldbalance)+'\\n')\n",
+ "\tfpt.write(str(obj.newbalance)+'\\n')\n",
+ "\tfpt.write(str(obj.payment)+'\\n')\n",
+ "\tfpt.write(str(obj.lastpayment.month)+' ')\n",
+ "\tfpt.write(str(obj.lastpayment.day)+' ')\n",
+ "\tfpt.write(str(obj.lastpayment.year)+'\\n\\n')\n",
+ "\t\n",
+ "\treturn\t\n",
+ "\t\n",
+ "fpt=open('records.txt','w')\n",
+ "\n",
+ "\n",
+ "customer=[]\n",
+ "\n",
+ "name='Steve Johnson'\n",
+ "street='123 Mountainview Drive '\n",
+ "city='Denver . CO'\n",
+ "acct_no=4208\n",
+ "oldbalance=247.88\n",
+ "payment=25.00\n",
+ "lastpay=date(6,14,1998)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "name='Susan Richards'\n",
+ "street='4383 Aligator Blvd'\n",
+ "city='Fort Lauderdale. FL'\n",
+ "acct_no=2219\n",
+ "oldbalance=135.00\n",
+ "payment=135.00\n",
+ "lastpay=date(8,10,2000)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "name='Martin Peterson'\n",
+ "street='1787 Pacific Parkway'\n",
+ "city='San Diego. CA'\n",
+ "acct_no=8452\n",
+ "oldbalance=387.42\n",
+ "payment=35.00\n",
+ "lastpay=date(9,22,1999)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "name='Phyllis Smith'\n",
+ "street='1000 Great White Way'\n",
+ "city='New York. NY'\n",
+ "acct_no=711\n",
+ "oldbalance=260.00\n",
+ "payment=0.00\n",
+ "lastpay=date(11,27,2001)\n",
+ "customer.append(account(name,street,city,acct_no,oldbalance,payment,lastpay))\n",
+ "\n",
+ "\n",
+ "\n",
+ "for i in range(0,4):\n",
+ "\tif customer[i].payment>0:\n",
+ "\t\tif customer[i].payment<0.1*customer[i].oldbalance:\n",
+ "\t\t\tcustomer[i].acct_type='O'\n",
+ "\t\telse:\n",
+ "\t\t\tcustomer[i].acct_type='C'\n",
+ "\telse:\n",
+ "\t\tif customer[i].oldbalance>0:\n",
+ "\t\t\tcustomer[i].acct_type='D'\n",
+ "\t\telse:\n",
+ "\t\t\tcustomer[i].acct_type='C'\n",
+ "\t\n",
+ "\tcustomer[i].newbalance=customer[i].oldbalance-customer[i].payment\n",
+ "\t\n",
+ "\twritefile(customer[i],fpt)\n",
+ "\n",
+ "fpt.close()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 2
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "<h3>Example 13.13, Page 13.28<h3>"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# concept of binary files\n",
+ "\n",
+ "fp=open('binval.txt','wb')\n",
+ "\n",
+ "for i in range(1000,11001):\n",
+ "\tfp.write(str(i))\n",
+ "\n",
+ "fp.close()\n"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 3
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file |