- class Program
-
{
-
static byte[] asyncbytes;
-
static FileStream fileS;
-
static FileInfo file;
-
-
static void Main(string[] args)
-
{
-
-
file = new FileInfo(@"c:\akshay.dat");
-
fileS= file.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
-
-
-
string str = "hello akshay joshi";
-
-
byte[] bytes = Encoding.Default.GetBytes(str);
-
fileS.Write(bytes, 0, bytes.Length);
-
-
AsyncCallback callback = new AsyncCallback(func);
-
-
-
asyncbytes = new byte[18];
-
Console.WriteLine("\n\nBegin reading....");
-
fileS.BeginRead(asyncbytes, 0, 18, callback, asyncbytes);
-
Console.ReadKey();
-
}
-
static void func(IAsyncResult result)
-
{
-
Console.WriteLine("Read.");
-
fileS.EndRead(result);
-
foreach (byte b in (byte[])result.AsyncState)
-
Console.Write((char)b);
-
}
-
}
Why only spaces are getting printed instead of "hello akshay joshi" ?
I can open the dat file and see "hello akshay joshi" is there !
3 1217 - class Program
-
{
-
static byte[] asyncbytes;
-
static FileStream fileS;
-
static FileInfo file;
-
-
static void Main(string[] args)
-
{
-
-
file = new FileInfo(@"c:\akshay.dat");
-
fileS= file.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);
-
-
-
string str = "hello akshay joshi";
-
-
byte[] bytes = Encoding.Default.GetBytes(str);
-
fileS.Write(bytes, 0, bytes.Length);
-
-
AsyncCallback callback = new AsyncCallback(func);
-
-
-
asyncbytes = new byte[18];
-
Console.WriteLine("\n\nBegin reading....");
-
fileS.BeginRead(asyncbytes, 0, 18, callback, asyncbytes);
-
Console.ReadKey();
-
}
-
static void func(IAsyncResult result)
-
{
-
Console.WriteLine("Read.");
-
fileS.EndRead(result);
-
foreach (byte b in (byte[])result.AsyncState)
-
Console.Write((char)b);
-
}
-
}
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[]
OOPS my bad, I forgot to set the position of stream to 0 before reading.
Problem solved !
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
by: TerryW |
last post by:
Does anyone have sample code to allow for a beginread from network
stream.
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |