File Handling In Pascal/ar: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: File Handling In Pascal/ar
| Old | New | Line | |
|---|---|---|---|
| 1 | + | <div dir="rtl" style="text-align: right;"> | |
| 2 | + | ||
| 3 | + | ## نظرة عامة | |
| 4 | + | ||
| 5 | + | يجب ان يتعلم جميع المبرمجين كيفية التعامل مع الملفات.تستخدم الملفات لحفظ المعلومات بحيث يمكن استرجاعها مرة اخرى | |
| 6 | + | ||
| 7 | + | ## الطريقة الهيكلية القديمة | |
| 8 | + | ||
| 9 | + | عند التعامل مع الملفات بطريق هيكلية (غير شيئية) يمكن تحديد نوع الملف حسب المحتويات التي نرغب بتخزينها( او المخزنة مسبقا) في الملف مثلا للنصوص نستخدم TextFile والذي يقوم بحفظ النص بشكل اسطر كل سطر ينتهي بعلامة انتهاء السطر([[End of Line|LineEnding]]). يمكنك حفظ مقاطع نصية (String)في هذا النوع من الملفات كما يمكن حفظ الارقام ايضا بعد تحوليها الى String. يمكنك انشاء نوعك الخاص من الملفات لحفظ نوع معين من البيانات :لتخزين الارقام فقط. هذا الملف يستخدم لحفظ ذلك النوع فقط مثال: | |
| 10 | + | ||
| 11 | + | <div dir="rtl" style="text-align: left;"> | |
| 12 | + | ||
| 13 | + | ``` pascal | |
| 14 | + | ... | |
| 15 | + | type | |
| 16 | + | TIntegerFile = file of integer; // يسمح لك فقط بكتابة ارقام Integer في الملف | |
| 17 | + | TExtendedFile = file of extended; // يسمح لك فقط بكتابة ارقام extended في الملف | |
| 18 | + | TCharFile = file of char; // يسمح لك فقط بكتابة حروف منفردة char في الملف | |
| 19 | + | ||
| 20 | + | //يمكن ايضا انشاء ملف من نوع سجل | |
| 21 | + | TCostumer = record | |
| 22 | + | CostumerName: string[255]; | |
| 23 | + | CostumerID: Integer; | |
| 24 | + | end; | |
| 25 | + | TCostumerFile = file of TCostumer; | |
| 26 | + | ``` | |
| 27 | + | ||
| 28 | + | <div dir="rtl" style="text-align: right;"> | |
| 29 | + | ||
| 30 | + | ## التعامل مع اخطاء الادخال و الاخراج | |
| 31 | + | ||
| 32 | + | العلم[I/O error handling flag](http://www.freepascal.org/docs-html/prog/progsu38.html#x45-440001.2.38)يخبر المصنف(compiler) بطريقة التعام مع الاخطاء اما برفع استثناء او حفظ النتيجة في اللمتغير IOResult | |
| 33 | + | ||
| 34 | + | <div dir="rtl" style="text-align: left;"> | |
| 35 | + | ||
| 36 | + | ``` pascal | |
| 37 | + | {$I+} // الاخطاء ستقوم برفع استثناء(الافتراضي) | |
| 38 | + | {$I-} //IOResult يتم وضع الخطأ في المتغير | |
| 39 | + | ``` | |
| 40 | + | ||
| 41 | + | <div dir="rtl" style="text-align: right;"> | |
| 42 | + | ||
| 43 | + | ## اجراءات الملفات | |
| 44 | + | ||
| 45 | + | اجلراءات ودوال التعامل مع الملفات التاية موجودة في الوحدة System. يمكنك الذهاب الى الوثائق للمزيد من المعلومات [Reference for 'System' unit](http://www.freepascal.org/docs-html/rtl/system/index-5.html). | |
| 46 | + | ||
| 47 | + | - **AssignFile** تحديد اسم الملف | |
| 48 | + | - **Append** فتح ملف موجود للاضافة اليه | |
| 49 | + | - **BlockRead** - قراءة معلوات من ملف غير محدد النوع الى الذاكرة | |
| 50 | + | - **BlockWrite** - كتابة معلومات من الذاكرة الى ملف غير محدد النوع | |
| 51 | + | - **CloseFile** اغلاق الملف | |
| 52 | + | - **EOF** - فحص نهاية الملف | |
| 53 | + | - **Erase** - حذف ملف من القرص | |
| 54 | + | - **FilePos** | |
| 55 | + | - **FileSize** - هذه الدالة تعطي حجم الملف | |
| 56 | + | - **Flush** - | |
| 57 | + | - **IOResult** - نتيج اخر عملية ادخال او اخراج | |
| 58 | + | - **Read** - القراءة من ملف | |
| 59 | + | - **ReadLn** - قراءة سطر من الملف | |
| 60 | + | - **Reset** - فغتح ملف للقراءة | |
| 61 | + | - **Rewrite** - انشاء ملف للكتابة | |
| 62 | + | - **Seek** - تغير المكان(مكان القراءة او الكتابة)في الملف | |
| 63 | + | - **SeekEOF** - الذهاب الى نهاية الملف | |
| 64 | + | - **SeekEOLn** - الذهاب الى نهاية السطر | |
| 65 | + | - **Truncate** - | |
| 66 | + | - **Write** - كتابة متغير في الملف | |
| 67 | + | - **WriteLn** - كتابة متغير في ملف نصي والذهاب الى ملف جديد | |
| 68 | + | ||
| 69 | + | ## مثال | |
| 70 | + | ||
| 71 | + | مثال لملف نصي TextFile | |
| 72 | + | ||
| 73 | + | <div dir="rtl" style="text-align: left;"> | |
| 74 | + | ||
| 75 | + | ``` pascal | |
| 76 | + | program CreateFile; | |
| 77 | + | ||
| 78 | + | uses | |
| 79 | + | Sysutils; | |
| 80 | + | ||
| 81 | + | const | |
| 82 | + | C_FNAME = 'textfile.txt'; | |
| 83 | + | ||
| 84 | + | var | |
| 85 | + | tfOut: TextFile; | |
| 86 | + | ||
| 87 | + | begin | |
| 88 | + | // Set the name of the file that will be created | |
| 89 | + | AssignFile(tfOut, C_FNAME); | |
| 90 | + | ||
| 91 | + | // Use exceptions to catch errors (this is the default so not absolutely requried) | |
| 92 | + | {$I+} | |
| 93 | + | ||
| 94 | + | // Embed the file creation in a try/except block to handle errors gracefully | |
| 95 | + | try | |
| 96 | + | // Create the file, write some text and close it. | |
| 97 | + | rewrite(tfOut); | |
| 98 | + | ||
| 99 | + | writeln(tfOut, 'Hello textfile!'); | |
| 100 | + | writeln(tfOut, 'The answer to life, the universe and everything: ', 42); | |
| 101 | + | ||
| 102 | + | CloseFile(tfOut); | |
| 103 | + | ||
| 104 | + | except | |
| 105 | + | // If there was an error the reason can be found here | |
| 106 | + | on E: EInOutError do | |
| 107 | + | writeln('File handling error occurred. Details: ', E.ClassName, '/', E.Message); | |
| 108 | + | end; | |
| 109 | + | ||
| 110 | + | // Give feedback and wait for key press | |
| 111 | + | writeln('File ', C_FNAME, ' created if all went ok. Press Enter to stop.'); | |
| 112 | + | readln; | |
| 113 | + | end. | |
| 114 | + | ``` | |
| 115 | + | ||
| 116 | + | --- | |
| 117 | + | *From the Free Pascal wiki, page "File Handling In Pascal/ar", as it stood on 2020-02-14 after a change by Trev, 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).* |
