Basic Pascal Tutorial/Chapter 2/Files/zh CN: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | 2D - 文件操作 (原作者: Tao Yue, 状态: 未更改) | |
| 2 | 2 | ||
| 3 | 3 | 读取文件,但不是从控制台(键盘)中读取数据,可以这样: | |
| 4 | 4 | ||
| 5 | 5 | ``` pascal | |
| 6 | 6 | read (文件变量, 参数列表); | |
| 7 | 7 | write (文件变量, 参数列表); | |
| 8 | 8 | ``` | |
| 9 | 9 | ||
| 10 | 10 | 同样用 `readln` 和 `writeln`。文件变量声明如下: | |
| 11 | 11 | ||
| 12 | 12 | ``` pascal | |
| 13 | 13 | var | |
| 14 | 14 | ... | |
| 15 | 15 | filein, fileout : text; | |
| 16 | 16 | ``` | |
| 17 | 17 | ||
| 18 | 18 | `text` 数据类型指定该文件仅是纯文本形式。 | |
| 19 | 19 | ||
| 20 | 20 | 操作文件时,需要先声明变量,以读取或者写入数据;我们需要将磁盘上的文件与变量相关联,可以使用以下2种方式之一,通常: | |
| 21 | 21 | ||
| 22 | - | ||
| 23 | - | ||
| 22 | + | ``` | |
| 23 | + | reset (文件变量, '文件名'); | |
| 24 | + | rewrite (文件变量, '文件名'); | |
| 25 | + | ``` | |
| 24 | 26 | ||
| 25 | 27 | `reset` 将对打开的文件进行读取或重写。 使用`reset`操作文件,只能进行`read`、`readln`操作。 使用`rewrite`操作文件,只能进行`write`、`rewrite`操作。 | |
| 26 | 28 | ||
| 27 | 29 | Turbo Pascal 使用引入赋值。首先将文件名赋值给一变量,之后调用`reset`或`rewrite`。 | |
| 28 | 30 | ||
| 29 | 31 | ``` pascal | |
| 30 | 32 | assign (文件变量, '文件名'); | |
| 31 | 33 | reset (文件变量); | |
| 32 | 34 | ``` | |
| 33 | 35 | ||
| 34 | 36 | 操作系统不同,路径表达方法也不一。Windows使用反斜杠(如 `c:\directory\name.pas`),而MacOS、Linux使用斜杠。 | |
| 35 | 37 | ||
| 36 | 38 | 操作完成后,你需要关闭它: | |
| 37 | 39 | ||
| 38 | 40 | ``` pascal | |
| 39 | 41 | close (文件标识符); | |
| 40 | 42 | ``` | |
| 41 | 43 | ||
| 42 | 44 | 下面是一个操作文件的示例,程序运行在DOS下,将读取file1.txt内容,写入到file2.txt中。 | |
| 43 | 45 | ||
| 44 | 46 | ``` pascal | |
| 45 | 47 | program CopyOneByteFile; | |
| 46 | 48 | ||
| 47 | 49 | var | |
| 48 | 50 | mychar : char; | |
| 49 | 51 | filein, fileout : text; | |
| 50 | 52 | ||
| 51 | 53 | begin | |
| 52 | 54 | assign (filein, 'c:\file1.txt'); | |
| 53 | 55 | reset (filein); | |
| 54 | 56 | assign (fileout, 'c:\file2.txt'); | |
| 55 | 57 | rewrite (fileout); | |
| 56 | 58 | read (filein, mychar); | |
| 57 | 59 | write (fileout, mychar); | |
| 58 | 60 | close(filein); | |
| 59 | 61 | close(fileout) | |
| 60 | 62 | end. | |
| 61 | 63 | ``` | |
| 62 | 64 | ||
| 63 | 65 | | | | | | |
| 64 | 66 | |----|----|----| | |
| 65 | 67 | | [[Basic Pascal Tutorial/Chapter 2/Formatting output/zh CN|上一页]] | [[Basic Pascal Tutorial/Contents/zh CN|目录]] | [[Basic Pascal Tutorial/Chapter 2/EOLN and EOF/zh CN|下一页]] | | |
| 66 | 68 | ||
| 67 | 69 | --- | |
| 68 | 70 | *From the Free Pascal wiki, page "Basic Pascal Tutorial/Chapter 2/Files/zh CN", as it stood on 2022-08-20 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
