Turbo pascal assign file




















Such files cannot be saved as plain text since extra data is needed to store the formatting. So other files may contain more data other than just text data.

Before we proceed with files, please make sure that you have enough hard disk space for our practice, since I am going to demonstrate to save a file to the hard disk!!

We only require a few bytes, don't worry! Reading a file in pascal is very easy. There are special functions in Pascal that enable us to read or write a text file.

It is worth taking a look at various important lines of the program. A new variable of type: Text is new to you, and this should be used whenever you are going to edit a text file. The variable FileName is needed to link to the file indicated by the user.

In order to read from the first line to the very last line of the file, it is recommended to use the repeat-until loop, ending the loop with : Until Eof UserFile , meaning: 'Until the E nd O f F ile [eof]'. In the above program, I am using the Writeln statement so that I can write to the file I have previously assigned. Don't confuse Writeln , with the ordinary stdout.

Note that this time the Writeln is taking two parameters instead of one and there will be no output to the screen, but the text is transferred to the file instead. The first parameter is the text file and the second one is the text to be written to the file on single line. The cursor moves to the next line just like when outputting on screen.

Open it and see what does it contain. When appending text to an existing file, it means that the file will be opened to write additional data, and exiting contents will not be overwritten. Try this program by copying into your Pascal compiler.

Run this program for two times or more. After you run this program several times, find the file named 'addtext. If I have misunderstood your question please post it differently. Dennis D. Turbo Pascal provides a built-in "string" type that makes this kind of work MUCH simpler than in Standard Pascal; here's a very simple especially, no error checking! This will work only if the file already exists or its size isn't zero, in those cases the program will create a run time error. To avoid those errors I usually use the following code You mustn't use rewrite filevar always, because, if used on an existing file, is will erase its contents.

Files with different filenames can still be used through command line parameters. Jump to: navigation , search. Details: ' , E. Press Enter to stop. Press enter to stop. Create Paramstr 2 , fmOpenwrite ; try outstream. WriteByte 68 ; msApp. WriteAnsiString 'Some extra text' ; msApp. Create ; FileStream. Create ; try MemBuffer. Free end ; begin If FileCopy fSource , fTarget then writeln 'File ' , fSource , ' copied to ' , ftarget else writeln 'File ' , fSource , ' not copied to ' , ftarget ; readln end.

Add 'An extra line appended to the text' ; slInfo. Add 'And another one. Add 'Let '' s stop here. We have just created and written into a file named students. Pascal allows file variables to be used as parameters in standard and user-defined subprograms.

The following example illustrates this concept. The program creates a file named rainfall. Next, it opens the file, reads the data and computes the average rainfall.



0コメント

  • 1000 / 1000