473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

BeginRead() function question

153 100+
Expand|Select|Wrap|Line Numbers
  1.     class Program
  2.     {
  3.         static byte[] asyncbytes;
  4.         static FileStream fileS;
  5.         static FileInfo file;
  6.  
  7.         static void Main(string[] args)
  8.         {
  9.  
  10.             file = new FileInfo(@"c:\akshay.dat");
  11.             fileS= file.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
  12.  
  13.  
  14.             string str = "hello akshay joshi";
  15.  
  16.             byte[] bytes = Encoding.Default.GetBytes(str);
  17.             fileS.Write(bytes, 0, bytes.Length);
  18.  
  19.             AsyncCallback callback = new AsyncCallback(func);
  20.  
  21.  
  22.             asyncbytes = new byte[18];
  23.             Console.WriteLine("\n\nBegin reading....");
  24.             fileS.BeginRead(asyncbytes, 0, 18, callback, asyncbytes);
  25.             Console.ReadKey();
  26.         }
  27.         static void func(IAsyncResult result)
  28.         {
  29.             Console.WriteLine("Read.");
  30.             fileS.EndRead(result);
  31.             foreach (byte b in (byte[])result.AsyncState)
  32.                 Console.Write((char)b);
  33.         }
  34.     }

Why only spaces are getting printed instead of "hello akshay joshi" ?
I can open the dat file and see "hello akshay joshi" is there !
Oct 3 '08 #1
3 1217
IanWright
179 100+
Expand|Select|Wrap|Line Numbers
  1.     class Program
  2.     {
  3.         static byte[] asyncbytes;
  4.         static FileStream fileS;
  5.         static FileInfo file;
  6.  
  7.         static void Main(string[] args)
  8.         {
  9.  
  10.             file = new FileInfo(@"c:\akshay.dat");
  11.             fileS= file.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
  12.  
  13.  
  14.             string str = "hello akshay joshi";
  15.  
  16.             byte[] bytes = Encoding.Default.GetBytes(str);
  17.             fileS.Write(bytes, 0, bytes.Length);
  18.  
  19.             AsyncCallback callback = new AsyncCallback(func);
  20.  
  21.  
  22.             asyncbytes = new byte[18];
  23.             Console.WriteLine("\n\nBegin reading....");
  24.             fileS.BeginRead(asyncbytes, 0, 18, callback, asyncbytes);
  25.             Console.ReadKey();
  26.         }
  27.         static void func(IAsyncResult result)
  28.         {
  29.             Console.WriteLine("Read.");
  30.             fileS.EndRead(result);
  31.             foreach (byte b in (byte[])result.AsyncState)
  32.                 Console.Write((char)b);
  33.         }
  34.     }

Why only spaces are getting printed instead of "hello akshay joshi" ?
I can open the dat file and see "hello akshay joshi" is there !
Do you mean to be reading the data as bytes? If so you aren't going to get textual output... If you want to be doing that read the data as a char[]
Oct 3 '08 #2
akshaycjoshi
153 100+
OOPS my bad, I forgot to set the position of stream to 0 before reading.
Problem solved !
Oct 3 '08 #3
Plater
7,872 Expert 4TB
OOPS my bad, I forgot to set the position of stream to 0 before reading.
Problem solved !
I was just going to say that. I'm too late though I guess.
Oct 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shane | last post by:
I want to use BeginRead on a socket stream. I want to abort the read if no data arrives within a certain time interval. I know i can't cancell the BeginRead directly but closing the socket seems...
4
by: 0to60 | last post by:
I have a class that wraps a TcpClient object and manages all the async reading of the socket. It works really nice, and I use it all over the place. But there's this ONE INSTANCE where I create...
2
by: DC | last post by:
Hi, I need to asynchronously read from a network (TCP) stream, and I am having trouble with retrieving whole blocks; I get a break in the data block every 1460 bytes which relates to network...
15
by: Lou | last post by:
I can't get it to work. Please help... -louie hPipe = CreateFile(connectionString, GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING,...
1
by: Manuel Costa | last post by:
Hi, i was testing a network application that i've been working on which use .net socket components. To read from a socket i use the networkstream beginread. If the other side dies, beginread...
6
by: Stephen Brooker | last post by:
Hi all, I've got a basic TCP app that is giving me trouble. I have a separate class that takes care of the TCP connection, and uses the NetworkStreams BeginRead and EndRead with a callback...
0
by: TerryW | last post by:
Does anyone have sample code to allow for a beginread from network stream.
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
1
by: ohmmega | last post by:
hello, i've a method in the form_load event witch calls 2 beginread statements (one after the other :) ) problem: during the second call it seem's that the callback won't be triggered. i've...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.