תרגיל בc#
שלום, התחלתי ללמוד קבצי טקסט בc#. וקיבלתי תרגיל שמשום מה לא מצליח לי התבקשתי לכתוב תכנית בעלת 2 פונקציות. א)קריאת קובץ ב)כתיבת קובץ 2. התכנית תציגלמשתמש תפריט ותשאל מה ברצונו לעשות (קריאה או כתיבה ובהתאם לכך תבקש את הקלט המתאים יש לכתוב שהפעולה התבצעה כהלכה. ניסיתי לפתור כך וזה לא הצליח לי, מי שמבין בזה, מוזמן להאיר את עיניי. תודה רבה וערב מצויין לכולם
שלום, התחלתי ללמוד קבצי טקסט בc#. וקיבלתי תרגיל שמשום מה לא מצליח לי התבקשתי לכתוב תכנית בעלת 2 פונקציות. א)קריאת קובץ ב)כתיבת קובץ 2. התכנית תציגלמשתמש תפריט ותשאל מה ברצונו לעשות (קריאה או כתיבה ובהתאם לכך תבקש את הקלט המתאים יש לכתוב שהפעולה התבצעה כהלכה. ניסיתי לפתור כך וזה לא הצליח לי, מי שמבין בזה, מוזמן להאיר את עיניי. תודה רבה וערב מצויין לכולם
using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { printmenu(); Console.WriteLine("choose an action: reading or writing"); int choice=int.Parse (Console.ReadLine ()); options (choice); } static void printmenu() { string menu="***menu****\n"; menu+="1.reading a file\n"; menu+="2.writing a file"; } static void options(int choice) { while (choice>2&&choice<1) { switch (choice) { case 1: { Console.WriteLine("enter file name"); string filename = Console.ReadLine(); Console.WriteLine(Readfile(filename)); break; } case 2: Console.WriteLine("enter file name"); string filename1 = Console.ReadLine(); Console.WriteLine("enter text into the file"); string text = Console.ReadLine(); Console.WriteLine(WriteFile(filename1, text)); break; default: Console.WriteLine("invalid number"); break; } } } static string Readfile(string filename) { StreamReader sr = new StreamReader(filename); string content = sr.ReadToEnd(); sr.Close(); return content; } static void WriteFile(string filename, string text) { StreamWriter sw = new StreamWriter(filename); sw.WriteLine(text); sw.Close(); } } }