BleepingComputer.com: Creates .txt then it wipes the first line?

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Creates .txt then it wipes the first line? Written in C#...

#1 User is offline   Wolfy87 

  • Senior Member
  • PipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 414
  • Joined: 25-July 08
  • Gender:Male
  • Location:England

  Posted 14 October 2008 - 11:49 AM

I am making a small game and working on the login screen and i have it to check for the save data (SD.txt), quest data (QD.txt) and bag data (BD.txt). if any of theese do not exist it creates them with the username as "default" and as level "1" etc. however, when trying to login or even loading the first line of SD.txt is wiped, this is where the login is stored and where it checkes to see if the name is correct. the only way it works is if i load it up, create a new account and then login but if i close then click debug again it wipes the user name and i cannot fine any code in it that could do it, here is the code for the first form (form1) wherin the console (con) resides, i just use this as a reference but it is not needed for any real processes.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FLUMPv1._0
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			login login = new login();
			con.Text += Environment.NewLine + "Initializing login screen";
			login.Show();
			if (File.Exists("SD.txt") == false)
				con.Text += Environment.NewLine + "Creating save data";
			if (File.Exists("QD.txt") == false)
				con.Text += Environment.NewLine + "Creating quest data";
			if (File.Exists("BD.txt") == false)
				con.Text += Environment.NewLine + "Creating bag data";
			TextWriter test = new StreamWriter("SD.txt");
			if (File.Exists("SD.txt") == false)
				test.WriteLine
					// username
					("username");
					test.WriteLine
					// level
					("1");
					test.WriteLine
					// money
					("0");
					test.WriteLine
					// xp
					("0");
					test.WriteLine
					// attrbiute
					("none");
			test.Close();
			TextWriter test2 = new StreamWriter("QD.txt");
			if (File.Exists("QD.txt") == false)
				test2.WriteLine
					// Quest 1
					("0");
					test2.WriteLine
					// Quest 2
					("0");
					test2.WriteLine
					// Quest 3
					("0");
					test2.WriteLine
					 // Quest 4
					("0");
					test2.WriteLine
					// Quest 5
					("0");
					test2.WriteLine
					// Quest 6
					("0");
					test2.WriteLine
					// Quest 7
					("0");
					test2.WriteLine
					// Quest 8
					("0");
					test2.WriteLine
					// Quest 9
					("0");
					test2.WriteLine
					// Quest 10
					("0");
			test2.Close();
			TextWriter bd = new StreamWriter("BD.txt");
			if (File.Exists("BD.txt") == false)
				bd.WriteLine
					// Apple
					("0");
					 bd.WriteLine
					// Potion
					("0");
					bd.WriteLine
					// Super potion
					("0");
					bd.WriteLine
					// Antidote
					("0");
					bd.WriteLine
					// Farcaster
					("0");
			bd.Close();
		}
	}
}


and here is my code for the login form...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FLUMPv1._0
{
	public partial class login : Form
	{
		public login()
		{
			InitializeComponent();
		}
		string[] username;
		game game = new game();

		private void login_Load(object sender, EventArgs e)
		{
			TextReader sc = new StreamReader("SD.txt");
			username = new string[1];			
			username[0] = sc.ReadLine();
			sc.Close();
			Form1 form1 = (Form1) Application.OpenForms["Form1"];			
			if (this.Visible == true)
				form1.con.Text += Environment.NewLine + "Login screen initialized";			
			if (File.Exists("SD.txt") == true)
				form1.con.Text += Environment.NewLine + "Save data file found";
			if (File.Exists("QD.txt") == true)
				form1.con.Text += Environment.NewLine + "Quest data file found";
			if (File.Exists("BD.txt") == true)
				form1.con.Text += Environment.NewLine + "Bag data file found";
			if (File.Exists("SD.txt") == false)
				form1.con.Text += Environment.NewLine + "Error: Save data file not found";
			if (File.Exists("QD.txt") == false)
				form1.con.Text += Environment.NewLine + "Error: Quest data file not found";
			if (File.Exists("BD.txt") == false)
				form1.con.Text += Environment.NewLine + "Error: Bag data file not found";
		}

		private void button2_Click(object sender, EventArgs e)
		{
			textBox1.Visible = false;
			button1.Visible = false;
			button2.Visible = false;
			textBox2.Visible = true;			
			button3.Visible = true;
			button4.Visible = true;
		}

		private void button3_Click(object sender, EventArgs e)
		{
			Form1 form1 = (Form1) Application.OpenForms ["Form1"];
			TextWriter create = new StreamWriter("SD.txt");
			create.WriteLine
			// username
			(textBox2.Text.ToString());
			create.WriteLine
			// level
			("1");
			create.WriteLine
			// money
			("0");
			create.WriteLine
			// xp
			("0");
			create.WriteLine
			// attrbiute
			("none");		   
			create.Close();
			form1.con.Text += Environment.NewLine + "Created account '"
				+ textBox2.Text + "' successfully";
			textBox1.Visible = true;
			button1.Visible = true;
			button2.Visible = true;
			textBox2.Visible = false;
			button3.Visible = false;
			button4.Visible = false;		 
		}

		private void button4_Click(object sender, EventArgs e)
		{
			textBox1.Visible = true;
			button1.Visible = true;
			button2.Visible = true;
			textBox2.Visible = false;			
			button3.Visible = false;
			button4.Visible = false;
		}
	   
		private void timer1_Tick(object sender, EventArgs e)
		{
			TextReader nr = new StreamReader("SD.txt");
			username[0] = (nr.ReadLine());
			nr.Close();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == username[0])
				game.Show();
			if (textBox1.Text == username[0])
				this.Close();
		}
	}
}


I am sorry it took uup so much room but it was the only way i could show you.
Thanks,
Wolfy87.

#2 User is offline   PlyPencil 

  • Member
  • PipPip
  • Find Topics
  • Group: Members
  • Posts: 51
  • Joined: 26-September 08

Posted 14 October 2008 - 12:23 PM

I encountered the same problem in VB with my terrain data (Wolfie will know what I mean)

The first 150 (line 1) lines were deleted every time I loaded the data, eventually pushing all the objects offscreen. What I did to fix this was told it to start with line 150 onwards. It still works now.

#3 User is offline   Wolfy87 

  • Senior Member
  • PipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 414
  • Joined: 25-July 08
  • Gender:Male
  • Location:England

Posted 15 October 2008 - 12:53 PM

Hmm... so shall i start with a load of twaddle (Ha ha twaddle [=) and then start reading after it? and if billie3 or groovicus is reading this, how can you read from a spcific line ie tr.Readile(23); is it somthing like that?

#4 User is online   groovicus 

  • Hail Groovicus!
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Moderator
  • Posts: 9,605
  • Joined: 05-June 04
  • Gender:Male
  • Location:Centerville, SD

Posted 15 October 2008 - 01:30 PM

We already went through an entire thread of how to read from text files. The answer is still the same. You can not just pick a line out of the middle of a file. You have to read it line by line until you get to the one that you want.
"Take the risk of thinking for yourself, much more happiness, truth, beauty, and wisdom will come to you that way" - Christopher Hitchens

#5 User is offline   Wolfy87 

  • Senior Member
  • PipPipPipPip
  • Find Topics
  • Group: Members
  • Posts: 414
  • Joined: 25-July 08
  • Gender:Male
  • Location:England

  Posted 16 October 2008 - 09:54 AM

Ok then no problem ile just have to do tr.ReadLine(); x 7 for example then do textBox2.Text = tr.ReadLine; but still, how do i overcome the deleted line should i just do what Ply did?

#6 User is online   Billy O'Neal 

  • Bleepin Engineer GRADUATE
  • PipPipPipPipPipPip
  • Find Topics
  • Group: Malware Response Instructor
  • Posts: 10,403
  • Joined: 17-January 08
  • Gender:Male
  • Location:Cleveland, Ohio

Posted 16 October 2008 - 07:35 PM

View PostWolfy87, on Oct 16 2008, 10:54 AM, said:

Ok then no problem ile just have to do tr.ReadLine(); x 7 for example then do textBox2.Text = tr.ReadLine; but still, how do i overcome the deleted line should i just do what Ply did?

From the API:
http://msdn.microsoft.com/en-us/library/sy...r.readline.aspx
the reader's position in the underlying Stream object is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the StreamReader object.

Billy3

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users