Writeln(myfile, 'Contact: Tutorials Point') Īppending to a file means writing to an existing file that already has some data without overwriting the file. Writeln(myfile, 'For details information on Pascal Programming') The following example creates and writes into a text file named contact.txt − You can declare and define such files as −ĭifference between a normal file of characters and a text file is that a text file is divided into lines, each terminated by a special end-of-line marker, automatically inserted by the system. Writeln('Average Rainfall: ', average:7:2) Ī text file, in Pascal, consists of lines of characters where each line is terminated with an end-of-line marker. Procedure computeAverage(var x: raindata) Please note that, if you use a file parameter with subprograms, it must be declared as a var parameter. Next, it opens the file, reads the data and computes the average rainfall. The program creates a file named rainfall.txt and stores some rainfall data. The following example illustrates this concept. Pascal allows file variables to be used as parameters in standard and user-defined subprograms. When the above code is compiled and executed, it produces the following result − Writeln('Batch Code: ', Student.s_batchcode) Now, let us write a program that would read the student's data from the file − We have just created and written into a file named students.dat. You can open the file using a text editor, like notepad, to look at John Smith's data. When compiled and run, the program would create a file named students.dat into the working directory. Student.s_batchcode := 'Computer Science' Student.s_addr := 'United States of America' It would create a file named students.dat and write a student's data into it − Let us write a program that would create a data file for students' records. Variables of a file type are created using the var declaration −įollowing are some examples of defining some file types and file variables −Īrrfile = file of array of integer The base type could be anything like, integer, real, Boolean, enumerated, subrange, record, arrays and sets except another file type. Where, the base-type indicates the type of the components of the file. A file's type is determined by the type of the components. Pascal treats a file as a sequence of components, which must be of uniform type.
0 Comments
Leave a Reply. |