summaryrefslogtreecommitdiff
path: root/Practical_C_Programming/Chapter_14_1.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Practical_C_Programming/Chapter_14_1.ipynb')
-rw-r--r--Practical_C_Programming/Chapter_14_1.ipynb98
1 files changed, 88 insertions, 10 deletions
diff --git a/Practical_C_Programming/Chapter_14_1.ipynb b/Practical_C_Programming/Chapter_14_1.ipynb
index b2876b44..6d3c736e 100644
--- a/Practical_C_Programming/Chapter_14_1.ipynb
+++ b/Practical_C_Programming/Chapter_14_1.ipynb
@@ -1,6 +1,7 @@
{
"metadata": {
- "name": "Chapter 14"
+ "name": "",
+ "signature": "sha256:aaf3603d1f605477459dd54990dcc4c8c154c9c808805468e92c30fde9487343"
},
"nbformat": 3,
"nbformat_minor": 0,
@@ -11,18 +12,42 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Chapter 14: File input/output"
+ "source": [
+ "Chapter 14: File input/output"
+ ]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 14.1, Page number: 253"
+ "source": [
+ "Example 14.1, Page number: 253"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 14.1.py\n# To count the number of characters in the file 'input.txt'\n\n\n# Variable declaration\nimport os\ncount = 0\nin_file = open ('input.txt', 'r')\n \n# Calculation and result\nif not os.path.exists ('input.txt') :\n print ('Cannot open input.txt\\n')\n\nwhile (1) :\n ch = in_file.read(1)\n if not ch :\n break\n count += 1\n\nprint ('Number of characters in input.txt is %d\\n' % count)\n\nin_file.close()",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "import os\n",
+ "count = 0\n",
+ "in_file = open ('input.txt', 'r')\n",
+ " \n",
+ "# Calculation and result\n",
+ "if not os.path.exists ('input.txt') :\n",
+ " print ('Cannot open input.txt\\n')\n",
+ "\n",
+ "while (1) :\n",
+ " ch = in_file.read(1)\n",
+ " if not ch :\n",
+ " break\n",
+ " count += 1\n",
+ "\n",
+ "print ('Number of characters in input.txt is %d\\n' % count)\n",
+ "\n",
+ "in_file.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -43,12 +68,29 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 14.3, Page number: 257"
+ "source": [
+ "Example 14.3, Page number: 257"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 14.3.py\n# To check whether a file exists or not\n\n\n# Variable declaration\nimport sys\nimport os\nin_file = open ('input.txt', 'r')\n \n# Calculation and result\nif not os.path.exists (name) :\n print ('Could not open file\\n')\n sys.exit(1)\nprint ('File found\\n')\n\nin_file.close()",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "import sys\n",
+ "import os\n",
+ "in_file = open ('input.txt', 'r')\n",
+ " \n",
+ "# Calculation and result\n",
+ "if not os.path.exists (name) :\n",
+ " print ('Could not open file\\n')\n",
+ " sys.exit(1)\n",
+ "print ('File found\\n')\n",
+ "\n",
+ "in_file.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [
@@ -69,12 +111,31 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 14.4, Page number: 260"
+ "source": [
+ "Example 14.4, Page number: 260"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 14.4.py\n# To write to a file\n\n\n# Variable declaration\nimport sys\nimport os\nout_file = open ('test.out', 'w')\n \n# Calculation and result\nif not os.path.exists ('test.out') :\n print ('Cannot open output file\\n')\n sys.exit(1)\n\nfor cur_char in range (0, 128) :\n out_file.write(str (cur_char))\n out_file.write('\\n')\n\nout_file.close()",
+ "input": [
+ "\n",
+ "# Variable declaration\n",
+ "import sys\n",
+ "import os\n",
+ "out_file = open ('test.out', 'w')\n",
+ " \n",
+ "# Calculation and result\n",
+ "if not os.path.exists ('test.out') :\n",
+ " print ('Cannot open output file\\n')\n",
+ " sys.exit(1)\n",
+ "\n",
+ "for cur_char in range (0, 128) :\n",
+ " out_file.write(str (cur_char))\n",
+ " out_file.write('\\n')\n",
+ "\n",
+ "out_file.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": [],
@@ -84,12 +145,29 @@
"cell_type": "heading",
"level": 3,
"metadata": {},
- "source": "Example 14.5, Page number: 267"
+ "source": [
+ "Example 14.5, Page number: 267"
+ ]
},
{
"cell_type": "code",
"collapsed": false,
- "input": "# Example 14.5.py\n# To copy the contents of a file to another file\n\n\n# Variable declaration\ntxt_file = open ('input.txt')\n\nsource_content = txt_file.read()\n\ntarget = open ('output.txt', 'w')\n\n# Calculation and result\ntarget.write(source_content)\nprint ('Content copied')\n\ntarget.close()",
+ "input": [
+ "\n",
+ "\n",
+ "# Variable declaration\n",
+ "txt_file = open ('input.txt')\n",
+ "\n",
+ "source_content = txt_file.read()\n",
+ "\n",
+ "target = open ('output.txt', 'w')\n",
+ "\n",
+ "# Calculation and result\n",
+ "target.write(source_content)\n",
+ "print ('Content copied')\n",
+ "\n",
+ "target.close()"
+ ],
"language": "python",
"metadata": {},
"outputs": []