Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 15th, 2004, 01:53 PM
Registered User
 
Join Date: Apr 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default ReadBlock(buffer1,index1,increment)

I am trying to use the method readblock, but I am confused.

Consider the code below:

FileStream fstream = new FileStream(filename, FileMode.Open);
StreamReader sreader = new StreamReader(fstream);


                int index1 = 0;
        int increment = 94;
        char[] buffer1 = ' ';
        string line_of_data = "";

        sreader.ReadBlock(buffer1,index1,increment);

My questions are:

How do I assign the results of the readblock?
I was thinking:
 buffer1 = sreader.ReadBlock(buffer1,index1,increment);

but, the results are already assigned in to buffer1 by method (I think).

What is the best way to convert the results to a string?

What kind of datatype is char[]?

Finally, why is this method returning an integer value?
Thanks,





Michael
 
Old April 21st, 2004, 03:04 PM
Registered User
 
Join Date: Apr 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael --

This code should do what you want:

                        char[] buffer1 = new char[200];
            int index1=0;
            int increment = 94;
            int charcount;
            string line_of_data;

            FileStream fs = new FileStream(filename,FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            charcount = sr.ReadBlock(buffer1,index1,increment);
            Console.WriteLine(charcount.ToString());
            Console.WriteLine(buffer1);


As you can see, charcount is set equal to the (integer) value returned by ReadBlock. This value equals the number of characters ReadBlock has loaded into buffer1 -- either the number you requested in increment, or the total number left in the file, whichever is less.

buffer1 is an array of characters (char[]) and, since it is an Array Object, can use any of the properties and methods of the Array Class. This includes subscripting -- buffer1[35] references the 36th character in the buffer (remember arrays start at 0!)

I don't know any easy way of converting a character array to a string in C#. They were the same thing in pre-.NET versions of C and C++, but I think that has changed in C#. You could probably brute-force build a string from char[] like this:

                        using System.Text;

                        StringBuilder sb = new StringBuilder();
            for (int i=0; i<buffer1.Length; i++)
                sb.Append(buffer1[i]);
            line_of_data = sb.ToString();
            Console.WriteLine(line_of_data);
            Console.ReadLine();

HTH,

Will





Similar Threads
Thread Thread Starter Forum Replies Last Post
Increment a tag stefan.yu XSLT 10 April 16th, 2007 05:45 AM
Auto increment prad_a MySQL 3 April 7th, 2007 05:47 AM
How to auto increment ? Shawn Mohan SQL Server 2000 2 June 22nd, 2006 03:00 AM
How to auto increment? Shawn Mohan ASP.NET 2.0 Basics 6 June 20th, 2006 10:36 PM
Increment date akibaMaila VB.NET 2002/2003 Basics 1 August 11th, 2005 11:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.