Computer Science AS CIE
-
data-representation as5 主题
-
multimedia as3 主题
-
compression as2 主题
-
networks-and-the-internet as11 主题
-
computers-and-components as5 主题
-
logic-gates-and-logic-circuits as2 主题
-
central-processing-unit-cpu-architecture as6 主题
-
assembly-language- as4 主题
-
bit-manipulation as1 主题
-
operating-systems as3 主题
-
language-translators as2 主题
-
data-security as3 主题
-
data-integrity as1 主题
-
ethics-and-ownership as3 主题
-
database-concepts as3 主题
-
database-management-systems-dbms- as1 主题
-
data-definition-language-ddl-and-data-manipulation-language-dml as1 主题
-
computational-thinking-skills as1 主题
-
algorithms as4 主题
-
data-types-and-records as2 主题
-
arrays as2 主题
-
files as1 主题
-
introduction-to-abstract-data-types-adt as1 主题
-
programming-basics as1 主题
-
constructs as2 主题
-
structured-programming as1 主题
-
program-development-life-cycle as1 主题
-
program-design- as2 主题
-
program-testing-and-maintenance as3 主题
file-handling as
Exam code:9618
Purpose of files
What is file handling?
-
File handling is the use of programming techniques to work with information stored in text files
-
Examples of file handing techniques are:
-
opening text files
-
reading text files
-
writing text files
-
closing text files
-
|
Concept |
CIE A Level Pseudocode |
|---|---|
|
Open file for reading |
|
|
Open file for writing |
|
|
Close file |
|
|
Read line |
|
|
Write line |
|
|
Check end of file |
|
|
Create new file |
|
|
Append to file |
|
-
An example program written in pseudocode to:
-
Opens a text file for reading
-
Reads each line (a fruit name)
-
Counts how many fruits are in the file
-
Outputs the total
-
Closes the file
-
DECLARE FruitName : STRING
DECLARE FruitCount : INTEGER FruitCount ← 0 OPENFILE FruitFile FOR READ WHILE NOT EOF(FruitFile) DO READFILE FruitFile, FruitName FruitCount ← FruitCount + 1
ENDWHILE CLOSEFILE FruitFile OUTPUT "Total number of fruits: ", FruitCount
Identifier table
|
Identifier |
Data type |
Description |
|---|---|---|
|
|
STRING |
Stores the name of each fruit read from the file |
|
|
INTEGER |
Keeps track of how many fruits have been read |
|
|
FILE |
The file containing the fruit names (e.g. “fruit.txt”) |
Worked Example
Write program code to read the contents of Data.txt into DataArray
Answer
-
Opening file Data.txt to read [1 mark]
-
Looping through all the 25/EOF … [1 mark]
-
… reading each line and storing/appending into array [1 mark]
-
Exception handling with appropriate output [1 mark]
-
Closing the file (in an appropriate place) [1 mark]
Example program code:
Java
Integer Counter = 0;
try{ Scanner Scanner1 = new Scanner(new File("Data.txt")); while(Scanner1.hasNextLine()){ DataArray[Counter] = Integer.parseInt(Scanner1.next()); Counter++; } Scanner1.close();
}catch(FileNotFoundException ex){ System.out.println("No data file found");
}
VB.NET
try Dim DataReader As New System.IO.StreamReader("Data.txt") Dim X As Integer = 0 Do Until DataReader.EndOfStream DataArray(X) = DataReader.ReadLine() X = X + 1 Loop DataReader.Close()
Catch ex As Exception Console.WriteLine("Invalid file")
End Try
Python
try: DataFile = open("Data.txt",'r') for Line in DataFile: DataArray.append(int(Line)) DataFile.close()
except IOError: print("Could not find file")
Responses