Working with strings (.txt files) and converting to arrays of chars

Working with strings (.txt files) and converting to arrays of chars

Page 1 of 1

5 Replies - 15007 Views - Last Post: 28 October 2010 - 11:23 AM Rate Topic: -----

#1 cmatija   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-October 10

Working with strings (.txt files) and converting to arrays of chars

Posted 28 October 2010 - 10:23 AM

O.K. In this tutorial I will teach you how to write(make) a .txt file, read from a .txt file and work with arrays currently in this example an array of chars


----------------------------------------------------------------------------------------------------------------------
Step 1: Plan the application(Determine what the app will do[have in mind your programming capabilities])

Step 2: Sketch out your app on a piece of paper or in some drawing prog. (I suggest to use the UML for diagrams)

Step 3: Make a rough make of your app.

Step 4: Refactor(refine) your code.

Step 5: Add explaining comments.
------------------------------------------------------------------------------------------------------------------------


I will make an example of how it should look like...
This tutorial will be constructed out of 2 parts...
1. Making a .txt file
2. Reading from it

Step 1: We will make a .txt file called Test.txt using the StreamWriter class(we will type in the console what we want to insert in the .txt file and then read from it and reverse it to the console window.

Step 2: Skipping this

Step (3)4:
We must first add an using statement: Add the line
using System.IO;
in the top part of your code.
//Part 1
            StreamWriter myWriter = new StreamWriter("Test.txt");

            Console.WriteLine("Type in the text to be inserted:");
            string text = Console.ReadLine();

            Console.WriteLine("Writing...");
            myWriter.WriteLine(text);
            Console.WriteLine("Finished.");

            myWriter.Close();

            //Part 2
            StreamReader myReader = new StreamReader("Test.txt");
            string line = " ";

            while (line != null)
            {
                line = myReader.ReadLine();

                if (line != null)
                {
                    char[] charArray = line.ToCharArray();
                    Array.Reverse(charArray);
                    Console.WriteLine("Now writing in reverse..");
                    Console.WriteLine(charArray);
                }
            }

            Console.ReadLine();



We used a StreamWriter to make the .txt file(its located in your Project locaton\bin\debug(release))
And the a StramReader to read from it. Then convert the string to charArray and finally reverse the array and print it to screen.

Good Luck and Happy Programming!

Is This A Good Question/Topic? 0
  • +

Replies To: Working with strings (.txt files) and converting to arrays of chars

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Working with strings (.txt files) and converting to arrays of chars

Posted 28 October 2010 - 10:41 AM

All you're doing is rehashing the MSDN tutorials in a play for points. We give this link out all the time to noobies.



Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.




I guess we don't have anyone actually checking the quality of our tutorials at the moment. I'm not an admin so I might be totally out of line with the thinking of DIC management. And if I am someone from admin please let me know to shut up or even delete this post entirely.

While we all appreciate your desire to jump in and help, tutorials really should reflect good programming habits that we want to foster in others. They should also cover a topic where there is a *need* and not just rehash the first 10 pages of every Intro to C# 101 textbook. And this isn't it. To me, this looks like a 60 second effort to earn kudo and rep points. Take a look at the quality of the existing tutorials and the subject matter they cover:


First, have enough pride in your work to proofread it

Quote

And the a StramReader


Second, if you are going to tell your readers that they need to do something as part of your tutorial I don't think you should be too lazy to not do it yourself.

Quote

Step 2: I will skip this part.

This post has been edited by tlhIn'toq: 28 October 2010 - 10:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Working with strings (.txt files) and converting to arrays of chars

Posted 28 October 2010 - 11:08 AM

This was not apparently submitted as a tutorial. I'm not sure if that's the intention or not. We check every tutorial that's submitted AS a tutorial, not only for plagiarism but for usefulness.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Working with strings (.txt files) and converting to arrays of chars

Posted 28 October 2010 - 11:10 AM

First, we do have a C# Tutorial Section. When you submit a tutorial there, the mods read over it, test it, and will either approve it, or reject it with a message explaining why it was rejected.

We also have a C# Snippets section, for things that are less than tutorials and more like interesting or useful code blocks. This is really more of a snippet than a tutorial.

Second, this is really basic stuff. As tlhIn'toq posted, it's pretty much a rehash of an existing MSDN example. No need to clog up with that kind of stuff. We're not in the business of teaching Hello World.

I would also suggest perusing the tutorials section. Read the work put into them, and use them as an example. It's good that you're trying to share your knowledge, but please try to keep it up to the high standards set by the other authors posting their work.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Working with strings (.txt files) and converting to arrays of chars

Posted 28 October 2010 - 11:13 AM

Ok. When I saw it start out with

Quote

O.K. In this tutorial I will teach you how to
just like the OP's last one (that someone *did* delete, thank-you) I didn't even check the section it was posted in.

At least this way the OP got some feedback.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Working with strings (.txt files) and converting to arrays of chars

Posted 28 October 2010 - 11:23 AM

Yeah, I missed that this was meant to be submitted as a tutorial in the first line :blush:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1