 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Sat Feb 09, 2008 3:13 pm Post subject: Reading/Writing Text with C#! |
|
|
Well, i've seen lots of tutorials being posted lately, so, i decided to throw a basic one in
Its kind of fun to mess around with, and i'll explain it step by step.
First off, we are writing to a text file called "MyTest.txt"
Now try and pick apart this code, then i'll do it for you...
| Code: |
using System;
using System.IO;
class Test
{
public static void Main()
{
// Assign the "path" variable the location of where it will be created
string path = @"c:\MyTest.txt";
// Check if that path exsists or not
// !File.Exsists(path) is saying that the path does not exsist
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
//The WriteLine Method write text to a new line.
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
// Open the file to read from.
using (StreamReader sr = File.OpenText(path))
{
// Set the variable "s" to an empty string
string s = "";
// Now we are saying that "s" equals what we wrote. And if it
// isn't empty, then display the text to the console.
while ((s = sr.ReadLine()) != null)
{
// Writing it to the console
Console.WriteLine(s);
// Make sure you read the line
// Ensures that the console doesn't just close
Console.ReadLine();
}
}
}
}
|
Yes, i did comment out most of it for you guys, but, it just makes it that much easier
Anyways, Notice that i referenced
IO stands for "Input/Output". We need this to be able to input text, and output the text on the console.
Well, basically the rest it commented out in the source. If you have any questions, feel free to ask...BUT IN THIS THREAD! NO PM'S!! >.<
Regards,
Overload~ |
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Feb 09, 2008 6:20 pm Post subject: |
|
|
I don't think the
is good practice. Why not just use
| Code: |
StreamWriter sw = ...
|
?
Also, you never close your little writers/readers, deallocate the resources, or close the handle. So this way, they'll have this open writer/reader until they restart, and because of that, use un-needed system resources and never be able to open the file.
Other than that, good job. _________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Sat Feb 09, 2008 6:37 pm Post subject: |
|
|
| samuri25404 wrote: | I don't think the
is good practice. Why not just use
| Code: |
StreamWriter sw = ...
|
?
Also, you never close your little writers/readers, deallocate the resources, or close the handle. So this way, they'll have this open writer/reader until they restart, and because of that, use un-needed system resources and never be able to open the file.
Other than that, good job. |
if I remember right, using automatically sets up a try / catch and tries to clean shit up when it's done. try / catch is fairly expensive, so it's usually better to try to watch for incorrect values rather than just use try / catch everywhere. |
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Sat Feb 09, 2008 7:23 pm Post subject: |
|
|
| You are correct. When you use the "using" command, it sets up the try/catch and cleans up the rest of the left over garbage. |
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Feb 09, 2008 8:18 pm Post subject: |
|
|
Ahh...
Ok, well good job! I actually learned something! (I usually don't get anything out of the tutorials posted around here.) _________________
|
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Sat Feb 09, 2008 10:20 pm Post subject: |
|
|
| samuri25404 wrote: | Ahh...
Ok, well good job! I actually learned something! (I usually don't get anything out of the tutorials posted around here.) |
lol, well, i am glad i helped you out  |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|