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

Reading From A Chosen Line...


  • Please log in to reply
18 replies to this topic

#1 Wolfy87

Wolfy87

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 19 September 2008 - 11:25 AM

Hi, in my game i am making i am saving all the save data to .txt files, i know how to do this but is there a better way ie by saving it all into one .txt and not hundreds then just reading from specific lines. Is this possible or should i just stick with writing about one word to each .txt and having loads of them to clog up the folder?

BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

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

Posted 19 September 2008 - 11:38 AM

Yes. You write all of the text to a single file, then when you need the information, you read the specific line that you want. It is a bit more complex than that, because you actually have to read the entire file, at least up to the line that you want. The typical way to do this is to read each line as one element of an array, then select the specific information that you want from an array.

#3 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 19 September 2008 - 11:43 AM

I was talking to my freind today who has self taught himself vb and in his game he is creating which you can see if you want i think hes got a website coleman systems or something. anyway, he searches the file when read for a specific word and then stops the sort of scan if you will, like you said, and then he must do something to the equivileant of Enviroment.NewLine; but in VB because he makes it read the line underneith the key word. so when im using the streamreader how do i tell it to look for a specificline, or where do i put the parameters. if you dont want to tell me that because im gessing your going to sed me off to the great programmer that is google instead of telling me so i can learn it for myself but cna you tell me this, how will i make it look for the line underneith onece (spelt wrong) it has found the sort of keyword or phrase?

#4 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 19 September 2008 - 11:48 AM

// create reader & open file
Textreader tr = new StreamReader("date.txt");

// read a line of text
Console.WriteLine(tr.ReadLine()); <<<<<<<<<< Would the params or keyword go in theese ()? Just after readline?

// close the stream
tr.Close();
if so then how would you use this the read the line underneith for the actual string? how would you say to read the line undernieth the keyword?

#5 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 19 September 2008 - 12:36 PM

I have got it to work like this...

private void button11_Click(object sender, EventArgs e)
{
TextWriter tw = new StreamWriter("MultiText.txt");
tw.WriteLine(textBox4.Text);
tw.WriteLine(textBox5.Text);
tw.WriteLine(textBox6.Text);
tw.Close();
}

private void button15_Click(object sender, EventArgs e)
{
TextReader tr = new StreamReader("MultiText.txt");
textBox4.Text = (tr.ReadLine());
textBox5.Text = (tr.ReadLine());
textBox6.Text = (tr.ReadLine());
tr.Close();
}
}
}

How do i tell it to read a specific line though?

#6 groovicus

groovicus

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

Posted 19 September 2008 - 01:27 PM

Perhaps use a 'for-loop' until you get to the line that you want? That is what I would do. Actually, I would erad all of the necessary parameters into an array (like I mentioned before) and then access them directly from the array as necessary so that I didn't have to keep reading the file over and over again. That way I could make changes to the values as necessary, and then just periodically re-write the data to the file.

#7 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 19 September 2008 - 01:52 PM

im sorry to say but because of me only understanding the basics and not knowing in depth about the C# language i do not fully understand what you are trying to say, from what i gather you are saying that i could use it on a contiuous reading and writing loop so it is constantly updating. i think i am compleatly wrong but i still dont understand how to read a specific line? Im sorry. :thumbsup:

#8 groovicus

groovicus

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

Posted 19 September 2008 - 02:53 PM

Do you know what an array is?
http://www.techotopia.com/index.php/Introd..._C_Sharp_Arrays

If the variable I need is on the 15th line, I merely retrieve the 15th element from the array (because my array already holds all of the information from the text file). If I need the 3rd element, I just get the third value from the array.

#9 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 20 September 2008 - 04:24 AM

I have read it and cant say i understand it, ive seen it used though. i will read it im more detale later but could you just say how you would create a array because i dont find it clear on the link. :thumbsup:

#10 groovicus

groovicus

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

Posted 20 September 2008 - 08:06 AM

A C# array may be created in a number of different ways. One way is to simply declare an array without initializing it with any values. The syntax for this is as follows:

type[] arrayname;


How much more clear can it be?

You know, we don't mind helping you tweak your code, but we absolutely do not have time to teach you basic data structures, or control structures, or how to use functions,etc. These are all of the most basic things you can do when programming, and there are thousands of samples available on the web. Those of us that do know how to program have spent a lot of time in classes or reading books in order to learn.

So the answer to your original question is that there is a way, but you won't be able to do it, and nobody really has the time to show you how. There are thousands of examples on the web that will help you do exactly what you want to do.

#11 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 20 September 2008 - 09:33 AM

Ok i undersatand and i know there are examples but i just dont understand them...i dont know what to put where it says arrayname or what it will do or how to use it. I just dont understand the examples.

#12 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 20 September 2008 - 09:53 AM

Ok, i have looked at it in more depth and i think i understand arrays a bit now so i could do like

string[] playerstats;
string[] playerstats = {"Username", "Level: 10", "Xp: 24", "Weapon"};
private void button1_Click(object sender.EventArgs e)
{
if (progressBar1.value == >1)
playerstats[2] = "Xp: 25";

etc etc.
but how would this sort of information be saved or recalled?

#13 groovicus

groovicus

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

Posted 20 September 2008 - 10:33 AM

THis line is redundant:
string[] playerstats;
All you ened is this:
string[] playerstats = {"Username", "Level: 10", "Xp: 24", "Weapon"};

If you know that he second element of the array contains the level, then there is no need to have the word "level" in it. Same with Experience points. All you really need is:
string[] playerstats = {"Username", "10", "24", "Weapon"};

You just showed that you have an example to set a value in an array:
playerstats[2] = "Xp: 25";

Except you would really just do this:
playerstats[2] = "25";

When you want to use a value from the array, you just use the element of the array that you want, ie:
String level= playerstats[2];
or:
String output = "The player's experience points are " + playerstats[2];

The rest you can figure out on your own.

i know there are examples but i just dont understand them


So then read them more until you do. Find other examples. This isn't meant to be understood in 5 minutes, or even 5 hours. If you have hammered away at something for two or three days, then you can say that you don't understand. Other than that, all you can truthfully say is that you have not really tried yet. There are things that I do that I sometimes have to tinker with for two weeks (and in one case, two months) before I understood how to do a process, test it, and then implement it. Sometimes that means creating some test code just to screw around with and see what happens. You are trying to learn incredibly basic processes while trying to do a very difficult project.

If you need to learn how to use arrays, put aside your game, create anew project, and spend a few hours testing out the code that you find on the Internet. You can't possibly understand how something works while it is part of a complex program. Once you practice using data structures or control loops in test applications, then you will be able to easily implement them in a larger program.

Figuratively speaking, you are tying to run a marathon, and you barely know how to tie your shoes. :thumbsup:

#14 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 21 September 2008 - 04:15 AM

:thumbsup: ok then and i have been doing that...sort of i just havent tryed this in my tester form (well...i call it that) yet. mostly because i dont have time im always out or doing homework but i should have some time today after i go shooting (air rifles). so is there a way of saving an arrays values, if there is then i will start looking aroung for a way to do it and ues it in my tester form to test it etc. i have dont this with all the other things like if statements and saving values to .txt etc. so i shall try this with arrays. thankyou for the advice.

#15 Wolfy87

Wolfy87
  • Topic Starter

  • Members
  • 414 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:England
  • Local time:09:57 AM

Posted 21 September 2008 - 11:34 AM

Erm, ive sort of hit a snag. in my tester form i made 2 text boxes and then a save and load button. you write in the text boxes then save then empty them and then click load and they are loaded from the array into the box but i cant get it to work:

private void button19_Click(object sender, EventArgs e)
{
string[] savetext;
savetext = new string[2];
savetext[0] = (textBox7.Text);
savetext[1] = (textBox8.Text);
}

private void button20_Click(object sender, EventArgs e)
{
textBox7.Text = savetext[0]; << savetext is underligned
textBox8.Text = savetext[1]; << savetext is underligned
}

Do i have to introduce it like you do in the firstplace, because i tryed that and couldnt get it to work?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users