Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Reading/Writing Text with C#!

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Overload
Master Cheater
Reputation: 0

Joined: 08 Feb 2008
Posts: 293

PostPosted: Sat Feb 09, 2008 3:13 pm    Post subject: Reading/Writing Text with C#! Reply with quote

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

Code:
using System.IO;


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
View user's profile Send private message MSN Messenger
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sat Feb 09, 2008 6:20 pm    Post subject: Reply with quote

I don't think the

Code:

using (..)


is good practice. Why not just use

Code:

StreamWriter sw = ...


Confused ?

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.

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
hcavolsdsadgadsg
I'm a spammer
Reputation: 26

Joined: 11 Jun 2007
Posts: 5801

PostPosted: Sat Feb 09, 2008 6:37 pm    Post subject: Reply with quote

samuri25404 wrote:
I don't think the

Code:

using (..)


is good practice. Why not just use

Code:

StreamWriter sw = ...


Confused ?

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
View user's profile Send private message
Overload
Master Cheater
Reputation: 0

Joined: 08 Feb 2008
Posts: 293

PostPosted: Sat Feb 09, 2008 7:23 pm    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
samuri25404
Grandmaster Cheater
Reputation: 7

Joined: 04 May 2007
Posts: 955
Location: Why do you care?

PostPosted: Sat Feb 09, 2008 8:18 pm    Post subject: Reply with quote

Ahh...

Ok, well good job! I actually learned something! (I usually don't get anything out of the tutorials posted around here.)

_________________
Wiccaan wrote:

Oh jeez, watchout I'm a bias person! Locked.


Auto Assembly Tuts:
In Depth Tutorial on AA
Extended
Back to top
View user's profile Send private message
Overload
Master Cheater
Reputation: 0

Joined: 08 Feb 2008
Posts: 293

PostPosted: Sat Feb 09, 2008 10:20 pm    Post subject: Reply with quote

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 Wink
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites