Question: I created two C# files. one to write(for user to enter info) and one to read. How do I fix the code so that the
I created two C# files. one to write(for user to enter info) and one to read. How do I fix the code so that the read program can read the txt files created by the write program
WriteInventory.cs
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO;
namespace WriteInventoryRecords { //create inventory class class inventory { public int itemid { get; set; } public string itemname { get; set; } public double itemprice { get; set; }
//start write inventory class class WriteInventoryrecords {
static void Main(string[] args) { const int End = 999; const string del = ""; const string filename = "details.txt"; inventory invent = new inventory(); FileStream outFile = new FileStream(filename, FileMode.Create, FileAccess.Write); StreamWriter writer = new StreamWriter(outFile); Console.Write("Enter Item No. Type " + End + " to quit......"); invent.itemid = Convert.ToInt32(Console.ReadLine()); while (invent.itemid != End) { Console.Write("Enter Item Name: "); invent.itemname = Console.ReadLine(); Console.Write("Enter Item Price: "); invent.itemprice = Convert.ToDouble(Console.ReadLine()); writer.WriteLine(invent.itemid + del + invent.itemname + del + invent.itemprice); Console.Write("Enter Next Item: Type " + End + " to quit..... "); invent.itemid = Convert.ToInt32(Console.ReadLine()); } writer.Close(); outFile.Close();
} } }
ReadInventory.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO;
namespace ReadInventoryRecords { class inventory { public int itemno { get; set; } public string itemname { get; set; } public double itemprice { get; set; }
class Program {
static void Main(string[] args) { const char del = ','; const string fname = "C:/details.txt"; inventory invent = new inventory(); FileStream filest = new FileStream(fname, FileMode.Open, FileAccess.Read); StreamReader read = new StreamReader(filest); string records; string[] fields; Console.WriteLine(" {0,-5}{1,12}{2,8} ", "ItemNo", "ItemName", "ItemPrice"); records = read.ReadLine(); while (records != null) { fields = records.Split(del); invent.itemno = Convert.ToInt32(fields[0]); invent.itemname = fields[1]; invent.itemprice = Convert.ToDouble(fields[2]); Console.WriteLine("{0,-5}{1,12}{2,8}", invent.itemno, invent.itemname.ToString(), invent.itemprice); records = read.ReadLine(); Console.ReadLine(); } read.Close(); filest.Close(); } } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
