Back to 课程

Computer Science AS CIE

0% Complete
0/0 Steps
  1. data-representation as
    5 主题
  2. multimedia as
    3 主题
  3. compression as
    2 主题
  4. networks-and-the-internet as
    11 主题
  5. computers-and-components as
    5 主题
  6. logic-gates-and-logic-circuits as
    2 主题
  7. central-processing-unit-cpu-architecture as
    6 主题
  8. assembly-language- as
    4 主题
  9. bit-manipulation as
    1 主题
  10. operating-systems as
    3 主题
  11. language-translators as
    2 主题
  12. data-security as
    3 主题
  13. data-integrity as
    1 主题
  14. ethics-and-ownership as
    3 主题
  15. database-concepts as
    3 主题
  16. database-management-systems-dbms- as
    1 主题
  17. data-definition-language-ddl-and-data-manipulation-language-dml as
    1 主题
  18. computational-thinking-skills as
    1 主题
  19. algorithms as
    4 主题
  20. data-types-and-records as
    2 主题
  21. arrays as
    2 主题
  22. files as
    1 主题
  23. introduction-to-abstract-data-types-adt as
    1 主题
  24. programming-basics as
    1 主题
  25. constructs as
    2 主题
  26. structured-programming as
    1 主题
  27. program-development-life-cycle as
    1 主题
  28. program-design- as
    2 主题
  29. program-testing-and-maintenance as
    3 主题
课 Progress
0% Complete

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

OPENFILE fruit.txt FOR READ

Open file for writing

OPENFILE Shopping.txt FOR WRITE

Close file

CLOSEFILE fruit.txt

Read line

READFILE fruit.txt, line

Write line

WRITE fruit.txt, "Oranges"

Check end of file

IF NOT EOF(fruit.txt) THEN

Create new file

OPENFILE Shopping.txt FOR WRITE (same as writing – it overwrites or creates)

Append to file

OPENFILE Shopping.txt FOR APPEND

  • 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

FruitName

STRING

Stores the name of each fruit read from the file

FruitCount

INTEGER

Keeps track of how many fruits have been read

FruitFile

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

您的邮箱地址不会被公开。 必填项已用 * 标注