vendredi 23 janvier 2015

Using Streamwriter to write to a specific place in a text file


I am trying to write to a specific position in a file. In the previous windows form I have selected a person to play with. In the file you will have three lines then a blank line then new player etc. I am a student and I cannot get this writing properly It is deleting everything in the text file. Can someone give me a few tips here because I cannot find any information on the internet to help me with writing to specific places in a text file. namespace Match_Picture {



public partial class Game : Form
{

string Temp_Tag = "";
int Number_of_Images = 0;
Label Clicked_Picture = new Label();
Label Temp_Pic = new Label();
int Num_Correct = 0;
int Timer = 0;
int Complete_Time = 7000;
int Time = 0;

public Game()
{
InitializeComponent();


}

struct Score
{
public string Name;
public int low;
public int high;
}

static Score[] scores;



private void BTN_StartGame_Click(object sender, EventArgs e)
{

lbl_1.Visible = true;
lbl_2.Visible = true;
lbl_3.Visible = true;
lbl_4.Visible = true;
lbl_5.Visible = true;
lbl_6.Visible = true;
lbl_7.Visible = true;
lbl_8.Visible = true;
lbl_9.Visible = true;
lbl_10.Visible = true;
lbl_11.Visible = true;
lbl_12.Visible = true;
lbl_13.Visible = true;
lbl_14.Visible = true;
lbl_15.Visible = true;
lbl_16.Visible = true;

Timer = 0;
tmr_Time_Taken.Enabled = true;

scores = new Score[4];

StreamReader sr = new StreamReader(@"..\..\..\Files\Match.txt");


for (int x = 0; x < 4; x++)
{
scores[x].Name = sr.ReadLine();
scores[x].low = Convert.ToInt32(sr.ReadLine());
scores[x].high = Convert.ToInt32(sr.ReadLine());
sr.ReadLine();
}
}

private void Picture_Click(object sender, EventArgs e)
{
Clicked_Picture = (Label)sender;

Number_of_Images++;
if (Number_of_Images < 3)
{
Clicked_Picture.Text = Clicked_Picture.Tag.ToString();

if (Number_of_Images == 1)
{
Temp_Tag = Clicked_Picture.Tag.ToString();
Temp_Pic = Clicked_Picture;
Clicked_Picture.Enabled = false;
}

else
{
if (Temp_Tag != Clicked_Picture.Tag.ToString())
{
tmr_Delay.Enabled = true;

Temp_Pic.Enabled = true;
}
else
{
Num_Correct++;
lbl_Matches.Text = Num_Correct.ToString();
if (Num_Correct == 8)
{
if (Timer < Complete_Time)
{
Complete_Time = Timer;
}

tmr_Time_Taken.Enabled = false;


Time = Complete_Time;

//MessageBox.Show("Congratulations, all matches complete");
MessageBox.Show(string.Format("Congratulations, all matches complete in {0} seconds", Time), "Results", MessageBoxButtons.OK);



StreamWriter sw = new StreamWriter(@"..\..\..\Files\Match.txt");

for (int x = 0; x < 4; x++)
{
if (Complete_Time < scores[x].low)
{
//string MyTime = Convert.ToString(Complete_Time);
scores[x].low = Complete_Time; //sw.Write(Complete_Time);
sw.Write(scores[x].low);

}
if (Complete_Time > scores[x].high)
{
//string MyTime = Convert.ToString(Complete_Time);
scores[x].high = Complete_Time; //sw.Write(Complete_Time);
sw.Write(scores[x].high);
//scores[x].high = Time;
//scores[x].high = Convert.ToInt32(sr.ReadLine());
}

}

}
Clicked_Picture.Enabled = false;
}
Number_of_Images = 0;
}
}
}




Aucun commentaire:

Enregistrer un commentaire