Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Creates .txt then it wipes the first line?


  • Please log in to reply
5 replies to this topic

#1 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:10:08 AM

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.

BC AdBot (Login to Remove)

 


#2 PlyPencil

PlyPencil

  • Members
  • 51 posts
  • OFFLINE
  •  
  • Local time:11:08 AM

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 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:10:08 AM

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 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:04:08 AM

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.

#5 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:10:08 AM

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 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:03:08 AM

Posted 16 October 2008 - 07:35 PM

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
Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users