תרגיל בc#

יטרנר

New member
תרגיל בc#

שלום, התחלתי ללמוד קבצי טקסט ב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(); } } }​
 

bursteg

New member
אני מניח שמדובר בקובץ text

לכן רצוי להשתמש ב- API שמתאים לקבצי טקסט. אפשר להשתמש ב- File.OpenText או להשתמש ב- TextReader במקום ב- StreamReader
 

אחיטופל

New member
../images/Emo92.gif

השורה הבאה גורמת לשגיאת קומפילציה
Console.WriteLine(WriteFile(filename1, text));​
התנאי שנמצא בלולאת ה while יגרום לכך שלא יכנסו ל switch ולא נראה לי שלזה התכוונתה.
 
למעלה