Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

A Level Computer Science Programming Guide/File Handling

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

Note for VB.NET users: While the methods used here are still valid, they are generally not used. They have been included in this guide as they are more similar to the Pseudocode file management, hopefully making them easier to understand. Even though it is not practice to use them, they should still be acceptable in an exam. Handling Text Files Opening Text Files LanguageGeneral UsageExample UsagePseudocodeOPENFILE <File Identifier> FOR <File Mode>OPENFILE Beans.txt FOR APPEND OPENFILE Styles.css FOR READ OPENFILE Names.tmp FOR WRITEVB.NETFileOpen(<File Number>], <File Identifier>, OpenMode.<File Mode>)FileOpen(17, “Beans.txt”, OpenMode.Append) FileOpen(FileNumber, “Styles.css”, OpenMode.Input) FileOpen(NamesFile, “Names.tmp”, OpenMode.Output) In VB.NET you can use the FreeFile() function to automatically obtain an unused file number, avoiding any problems that could occur if accidentally using the same number for two files.Dim FileNumber As Integer = FreeFile()It is good practice to always keep track of your file numbers, as it will help reduce bugs (aka forgetting or using the wrong file number), remove any Magic Numbers and make the code easier to read and understand. +File Mode UsageFile Mode IdentifiersDescriptionPseudocodeVB.NETAPPENDOpenMode.AppendUsed when Writing data to the file. If the file already exists, the new data will be added to the file after any existing data.READOpenMode.InputUsed when Reading data from the file.WriteOpenMode.OutputUsed when Writing data to the file. If the file already exists, the file will be deleted and a new file will be created with the new data. Reading From Text Files Once a file is opened in 'Read' mode, you can use the following commands to read the data from the files. LanguageGeneral UsageExample UsagePseudocodeREADFILE <File Identifier>, <Identifier>/Datatypes declared as strings READFILE Styles.css, NextLine READFILE TransactionHistory.txt, Transaction READFILE Address.db, CurrentAddress WRITEFILE <File identifier>, <Variable>VB.NET<Identifier> = LineInput(<File Identifier>)The File Identifer is the file number used identify the file when it was opened.NextLine = LineInput(StylesFileNumber) Transaction = LineInput(TransactionHistoryFileNumber) CurrentAddress = LineInput(AddressFileNumber) The Variable should be of data type STRING. This command reads the text file line by line. The EOF() function can be used in Pseudocode and VB.NET to determine whether the file pointer is at the end of the file. It returns a Boolean Value, and can be useful when implementing loops.EOF(<File Identifier>) +PseudocodeVB.NETEOF(file.txt)EOF(FileNumber) Writing To Text Files Once a file is opened in 'Write' or 'Append' mode, you can use the following commands to write the data to the files. LanguageGeneral UsageExample UsagePseudocodeREADFILE <File Identifier>, <Identifier>WRITEFILE Beans.txt, BeanData WRITEFILE Names.tmp, CurrentUser WRITEFILE Recipes.db, NewRecipeVB.NETPrintLine(<File Identifier>, <Identifier>)The File Identifer is the file number used identify the file when it was opened.PrintLine("Beans.txt", BeanData) PrintLine("Name.tmp", CurrentUser) PrintLine("Recipes.db" NewRecipe) The Variable should be of data type STRING. Closing Text Files LanguageGeneral UsageExample UsagePseudocodeCLOSEFILE <File identifier>CLOSEFILE Beans.txt CLOSEFILE Styles.css CLOSEFILE Names.tmpVB.NETFileClose(<File Identifier>)The File Identifer is the file number used identify the file when it was opened.FileClose(17) FileClose(FileNumber) FileClose(NamesFile) You should always remember to close your files. Failure to do so can result in major problems later on when any program tries to open the file, or the program tries to open or edit a different file with the same identifier.