MohammadZaidan فريق هندسة الحواسب
الجنس : عدد المساهمات : 829
| موضوع: #C برنامج للتخاطب ع ملفين باستخدام اكسبشن exeption 12/30/2010, 13:37 | |
| السؤال : بفرض أنه يوجد لدينا الملف النصي (c:\t.txt) يحتوي في كل سطر رموز محرفية وأرقام والمطلوب لإنشاء ملف نصي أول (c:\t1.txt) يحتوي فقط على نفس أسطر الملف (c:\t.txt) بدون أرقام وملف نصي ثاني c:\t2.txt) يحتوي فقط على نفس أسطر الملف (c:\t.txt) بدون أحرف. ملاحظة:عالج مشاكل الأخطاء زمن التشغبل. الحل على الشكل التالي : - الكود:
-
using System; using System.Collections.Generic; using System.Text; using System.IO;
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { StreamReader SR = new StreamReader("c:\\t.txt"); StreamWriter SW1 = new StreamWriter("c:\\t1.txt"); StreamWriter SW2 = new StreamWriter("c:\\t2.txt"); if (SR == null || SW1 == null || SW2 == null) throw (new Exception("Error 1"));
try { String Line = SR.ReadLine();
while (Line != null) {
for (int i = 0; i < Line.Length; i++) if (Line[i] >= '0' && Line[i] <= '9') SW1.Write(Line[i]); else SW2.Write(Line[i]); Line = SR.ReadLine(); SW1.WriteLine(); SW2.WriteLine();
} } catch (Exception ex1) { Console.WriteLine(ex1.Message); } finally { Console.WriteLine("Close Files");
} SR.Close(); SW1.Close(); SW2.Close();
} catch (Exception ex2) { Console.WriteLine(ex2.Message); } finally { Console.WriteLine("End"); } } } }
| |
|