Manipulating a string read as a line from a file : Ascii String Read Write « File Stream « C# / C Sharp

C# / C Sharp
1. 2D Graphics
2. Class Interface
3. Collections Data Structure
4. Components
5. Data Types
6. Database ADO.net
7. Design Patterns
8. Development Class
9. Event
10. File Stream
11. Generics
12. GUI Windows Form
13. Language Basics
14. LINQ
15. Network
16. Office
17. Reflection
18. Regular Expressions
19. Security
20. Services Event
21. Thread
22. Web Services
23. Windows
24. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / C Sharp » File Stream » Ascii String Read WriteScreenshots 
Manipulating a string read as a line from a file

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
//  Builder.cs -- Demonstrates manipulating a string read as a line from a file
//                Compile this program with the following command line:
//                    C:>csc Builder.cs
//
namespace nsBuilder
{
    using System;
    using System.Text;
    using System.IO;
    public class Builder
    {
        static public int Main ()
        {
// Create a stream object and open the input file
            FileStream istream;
            try
            {
                istream = new FileStream ("sample.txt", FileMode.Open, FileAccess.Read);
            }
            catch (Exception)
            {
                Console.WriteLine ("Could not open sample.txt for reading");
                return (-1);
            }
// Associate a reader with the stream
            StreamReader reader = new StreamReader (istream);
// Declare a new StringBuilder
            StringBuilder strb = new StringBuilder ();
// Counter for the lines
            int Lines = 0;
            while (reader.Peek() 0)
            {
                ++Lines;
// Clear out the string builder
                strb.Length = 0;
// Read a line from the file
                string str = reader.ReadLine();
// Split the line into words
                string [] Data = str.Split (new char [] {' '});
// Build the output line to show line, word and character count
                strb.AppendFormat ("Line {0} contains {1} words and {2} characters:\r\n{3}", Lines, Data.Length, str.Length, str);
// Write the string to the console
                Console.WriteLine (strb.ToString ());
            }
            istream.Close ();
            return (0);
        }
    }
}

//File: sample.txt
/*
The quick red fox jumps over the lazy brown dog.
Now is the time for all good men to come to the aid of their Teletype.
Peter Piper picked a peck of peppered pickles.


*/

           
       
Related examples in the same category
1.  Demonstrate StringReader and StringWriter  Demonstrate StringReader and StringWriter
2. Reads an ASCII encoded file and writes the text to another file in wide character format
3. Using String ReaderUsing String Reader
4. An enhanced cipher component that maintains a log fileAn enhanced cipher component that maintains a log file
5. Text ReaderText Reader
w__w__w__.__j__av_a__2_s_.c_o___m__ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.