0

i wanted to update a line in a text file. the text file contents are just numbers and is separated by a new line. each line is assigned to one value. i need to update each line every time the value is updated. to know what line i should update i used an if and else if statements to separate the updating for each line. i was able to write on it but after writing on the text file the contents on it is be deleted. i tried moving my declaration for the streamwriter and streamreader but thetext file can only be accessed once. here is my code. please help me

try
{
    StreamReader sr = new StreamReader(DEFINES.DISPENSER_STORAGE);
    List<string> lines = new List<string>();
    while (!sr.EndOfStream)
        lines.Add(sr.ReadLine());
    Console.WriteLine("fifty" + lines[0]);
    Console.WriteLine("twenty" + lines[1]);
    Console.WriteLine("ten" + lines[2]);
    Console.WriteLine("five" + lines[3]);
    Console.WriteLine("one" + lines[4]);
    sr.Close();
    StreamWriter sw = new StreamWriter(DEFINES.DISPENSER_STORAGE);
    if (flag == DEFINES.UPDATE_FIFTY)
    {
        string fifty_left = (Convert.ToString(Convert.ToInt32(
              cash_update_local.Get(DEFINES.FIFTY_BILL_COUNT)) -
              Convert.ToInt32(cash_dispenser.Get(
              DEFINES.num_of_fifty_to_dipense))));
        lines[0] = fifty_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("fifty" + lines[0]);
        sw.Close();
    }
    else if (flag == DEFINES.UPDATE_TWENTY)
    {

        string twenty_left = (Convert.ToString(Convert.ToInt32(
            cash_update_local.Get(DEFINES.TWENTY_BILL_COUNT)) -
            Convert.ToInt32(cash_dispenser.Get(
            DEFINES.num_of_twenty_to_dipense))));
        lines[1] = twenty_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("twenty" + lines[1]);
        sw.Close();
    }
    else if (flag == DEFINES.UPDATE_TEN)
    {
        string ten_left = (Convert.ToString(Convert.ToInt32(
            cash_update_local.Get(DEFINES.TEN_COIN_COUNT)) -
            Convert.ToInt32(cash_dispenser.Get(
            DEFINES.num_of_ten_to_dipense))));
        lines[2] = ten_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("ten" + lines[2]);
        sw.Close();

    }
    else if (flag == DEFINES.UPDATE_FIVE)
    {

        string five_left = (Convert.ToString(Convert.ToInt32(
            cash_update_local.Get(DEFINES.FIVE_COIN_COUNT)) -
            Convert.ToInt32(cash_dispenser.Get(
            DEFINES.num_of_five_to_dipense))));
        lines[3] = five_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("five" + lines[3]);
        sw.Close();
    }
    else if (flag == DEFINES.UPDATE_ONE)
    {
        string one_left = (Convert.ToString(Convert.ToInt32(
            cash_update_local.Get(DEFINES.ONE_COIN_COUNT)) -
            Convert.ToInt32(cash_dispenser.Get(
            DEFINES.num_of_one_to_dipense))));
        lines[4] = one_left;
        foreach (string s in lines)
        {
            sw.WriteLine(s);
        }
        MessageBox.Show("one" + lines[4]);
        sw.Close();
    }
    return return_status;
}

0 Answers0