Chapter 6 Persistence-Saving and Retrieving Data

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
More on PHP Coding Lab no. 6 Advance Database Management System.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CHAPTER 6 FILE PROCESSING. 2 Introduction  The most convenient way to process involving large data sets is to store them into a file for later processing.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Python File Handling. In all the programs you have made so far when program is closed all the data is lost, but what if you want to keep the data to use.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patterson and Hennessy Text.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Regular Expression (continue) and Cookies. Quick Review What letter values would be included for the following variable, which will be used for validation.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
Aniket Joshi Justin Thomas. Agenda Introduction to SQL Injection SQL Injection Attack SQL Injection Prevention Summary.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
1 Chapter 7 – Object-Oriented Programming and File Handling spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Storing and Retrieving Data
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
Advanced Web 2012 Lecture 6 Sean Costain Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing.
Pascal Programming Today Chapter 11 1 Chapter 11.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
WinCvs. WinCVS WinCvs is a window based version control system. Use WinCvs when  You want to save every version of your file you have ever created. CVS.
Files Tutor: You will need ….
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Files A collection of related data treated as a unit. Two types Text
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
COMPUTER PROGRAMMING Year 9 – lesson 1. Objective and Outcome Teaching Objective We are going to look at how to construct a computer program. We will.
FILE I/O: Low-level 1. The Big Picture 2 Low-Level, cont. Some files are mixed format that are not readable by high- level functions such as xlsread()
GNG1106 Lab # 8 C Structures and Files Slides by: Mohamad Eid Contributors: Diana Inkpen, Alan Williams, Daniel Amyot.
Chapter 14: Sequential Access Files
CS 106A, Lecture 4 Introduction to Java
Chapter 9: Value-Returning Functions
Chapter 3 Program Design
User-Written Functions
Introduction to Visual Basic 2008 Programming
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Chapter 2 Client/Server Applications
TMF1414 Introduction to Programming
Algorithms Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
File I/O.
Arrays and files BIS1523 – Lecture 15.
Functions CIS 40 – Introduction to Programming in Python
Intro to PHP & Variables
File Input/Output.
Files I/O, Streams, I/O Redirection, Reading with fscanf
File I/O We are used to reading from and writing to the terminal:
Learning to Program in Python
Chapter 11 – File Processing
Number and String Operations
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Input and Output.
File Handling.
Web Programming Language
Intro to PHP.
File Handling in C.
Reading from and Writing to Files
User Input Keyboard input.
Chapter 11 Files chap8.
ICS103: Programming in C 6: Pointers and Modular Programming
Reading from and Writing to Files
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

Chapter 6 Persistence-Saving and Retrieving Data 954240 – Web Programming

Introduction In this chapter you will learn to distinguish between temporary (transient) data and data that is stored (persistent data), and compare the use of text files and databases for persistent data storage. You will learn the basic operations needed to process text files, and implement these operations using PHP functions to read, write and append text files located on a Web server. You will also learn how to extract (parse) multiple data values from a single line that has been read from a text file.

The Difference Between Persistent and Transient Data Persistent Data is known as data whose life extends beyond the lifetime of any of the program. Persistent Data is stored so that it can be accessed and modified by programs as needed. Transient Data is known as the world of computing , data that is used and then forgotten. More precisely transient data refers to data that is produced while an application is running and lost when the application ends

The Difference Between Persistent and Transient Data Many programs work with a combination of transient and persistent data. For example, you might write a program that asks the user for information of some kind and then stores the information in a file for later use. In this case the user inputs and screen displays may be considered transient data, while the data that the program writes to the file is persistent since it will remain in the file after the program ends.

Working with Text File In order to work with a text file, a program must first open the file. There are basically three ways to open a text file: Opening a file for Read Operations Opening a File for Write Operations Opening a File for Append Operations

Working with Text File Opening a file for Read Operations Assuming the file exists, this operation opens the file and positions a read pointer at the beginning of the file. Opening a File for Write Operations You will open a file for write operations when you want to replace data in an existing file with new data. Opening a File for Append Operations You will open a file for append operations when you want to add data to an existing file.

Closing a Text File When a program that has opened a file no longer needs to access it, the file should be closed. The close operation places an End-Of-File (EOF) marker at the end of the file and releases the file for access by other programs.

Reading Data from a Text File Let’s start with an example where we open a text file, read data from the file, close the file, and then process the data. Consider a file named scores.txt that contains five scores on separate lines as follows: 89 77 92 69 87

Reading Data from a Text File Here is a program requirement to process the scores.txt file: Here is a solution algorithm for averageScore.php: averageScore requirement: Read the five scores from the scores.txt file, then calculate and display the average score. averageScore.php algorithm: Open scores.txt as scoresFile for reading Read scorel, score2, score3, score4, score5 from scoresFile Close scoresFile avgScore = (scorel+ score2+ score3+ score4+ score5) / 5 Display averageScore END

Reading Data from a Text File Open : use Open to indicate that the program must open a file and specify whether the file is to be opened for reading, writing or appending Read : use Read to indicate that the program must read a value from a file into a variable. Close : use Close to indicate that the program must close a file.

PHP Functions to Read Data from a Text File Hear is the PHP code for the averageScore algorithm: <?php $scoresFile = fopen (“scores.txt”,”r”); $score1 = fgets ($scoresFile); $score2 = fgets ($scoresFile); $score3 = fgets ($scoresFile); $score4 = fgets ($scoresFile); $score5 = fgets ($scoresFile); fclose ($scoresFile); $avgScore = ($score1 + $score2 + $score3 + $score4 + $score5) / 5; echo (“<h1>AVERAGE SCORE</h1>); echo (“<p>The average score is $avgScore.</p>”); echo (“<p><a href=\”averageScore.html\”> Return to averageScore form</a></p>); ?>

PHP Functions to Read Data from a Text File $scoresFile = fopen(“scores.txt”, “r”); This instruction uses the PHP fopen( ) function to open a file. The first parameter “scores.txt” indicates the name of the file to open. The second parameter “r” in this case the file is to be opened for reading (“r”), with the read pointer set to the beginning of the file. $score1 = fgets ($scoresFile); The fgets( ) function reads and returns the next line from the file.

PHP Functions to Read Data from a Text File Mode Meaning r Open file for reading only r+ Open file for reading and writing w Open file for writing to replace existing data if no existing file, it will create new file. w+ Open file for reading to replace existing data if no existing file, it will create new file. a Open file for writing at the end of existing file if no existing file, it will create new file. a+ Open file for reading at the end of existing file if no existing file, it will create new file.

PHP Functions to Read Data from a Text File fclose ($scoresFile); The fclose( ) function is used to close the file. It is good programming practice to close a file as soon as your program has finished using it. This ensures that the file is available for processing by other programs as soon as possible.

PHP Functions to Read Data from a Text File

PHP Functions to Write Data to a Text File fputs ($scoresFile, “$score1\n”); The fputs( ) function is used to write data to a file The \n represents the new line character and is an example of an escape character. fputs ($scoresFile, “$score1\n$score2\n$score3\n$score4\n$score5\n”); The value from each variable is written to the file followed by a new line marker, so the values are written on five separate lines.

Using Escape Characters Here are the most commonly used escape characters: \t generates a tab \n starts a new line \” generates a double quote “ \’ generates a single quote ‘ \\ generates a back slash \