Sign in Join
Wiki copytemporary

Revision 1 of 2, by Trev on 2-14-2020. The page as it is now.

File Handling In Pascal/ar

<div dir="rtl" style="text-align: right;">

نظرة عامة

يجب ان يتعلم جميع المبرمجين كيفية التعامل مع الملفات.تستخدم الملفات لحفظ المعلومات بحيث يمكن استرجاعها مرة اخرى

الطريقة الهيكلية القديمة

عند التعامل مع الملفات بطريق هيكلية (غير شيئية) يمكن تحديد نوع الملف حسب المحتويات التي نرغب بتخزينها( او المخزنة مسبقا) في الملف مثلا للنصوص نستخدم TextFile والذي يقوم بحفظ النص بشكل اسطر كل سطر ينتهي بعلامة انتهاء السطر(LineEnding). يمكنك حفظ مقاطع نصية (String)في هذا النوع من الملفات كما يمكن حفظ الارقام ايضا بعد تحوليها الى String. يمكنك انشاء نوعك الخاص من الملفات لحفظ نوع معين من البيانات :لتخزين الارقام فقط. هذا الملف يستخدم لحفظ ذلك النوع فقط مثال:

<div dir="rtl" style="text-align: left;">

pascal
...
type
  TIntegerFile  = file of integer;  // يسمح لك فقط بكتابة ارقام Integer في الملف
  TExtendedFile = file of extended; // يسمح لك فقط بكتابة ارقام extended في الملف
  TCharFile     = file of char;     //  يسمح لك فقط بكتابة حروف منفردة char في الملف 

//يمكن ايضا انشاء ملف من نوع سجل 
TCostumer = record
    CostumerName: string[255];
    CostumerID: Integer;
end;
TCostumerFile     = file of TCostumer;

<div dir="rtl" style="text-align: right;">

التعامل مع اخطاء الادخال و الاخراج

العلمI/O error handling flagيخبر المصنف(compiler) بطريقة التعام مع الاخطاء اما برفع استثناء او حفظ النتيجة في اللمتغير IOResult

<div dir="rtl" style="text-align: left;">

pascal
{$I+} // الاخطاء ستقوم برفع استثناء(الافتراضي)
{$I-} //IOResult يتم وضع الخطأ في المتغير

<div dir="rtl" style="text-align: right;">

اجراءات الملفات

اجلراءات ودوال التعامل مع الملفات التاية موجودة في الوحدة System. يمكنك الذهاب الى الوثائق للمزيد من المعلومات Reference for 'System' unit.

مثال

مثال لملف نصي TextFile

<div dir="rtl" style="text-align: left;">

pascal
program CreateFile;

uses
 Sysutils;

const
  C_FNAME = 'textfile.txt';

var
  tfOut: TextFile;

begin
  // Set the name of the file that will be created
  AssignFile(tfOut, C_FNAME);

  // Use exceptions to catch errors (this is the default so not absolutely requried)
  {$I+}

  // Embed the file creation in a try/except block to handle errors gracefully
  try
    // Create the file, write some text and close it.
    rewrite(tfOut);

    writeln(tfOut, 'Hello textfile!');
    writeln(tfOut, 'The answer to life, the universe and everything: ', 42);

    CloseFile(tfOut);

  except
    // If there was an error the reason can be found here
    on E: EInOutError do
      writeln('File handling error occurred. Details: ', E.ClassName, '/', E.Message);
  end;

  // Give feedback and wait for key press
  writeln('File ', C_FNAME, ' created if all went ok. Press Enter to stop.');
  readln;
end.

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.