rookie_rockie

hi, i've created the following codes in the console application and it compiled just fine
Code Snippet


{
byte[] byteData = null;
byte[] byteTemp = new byte[512];/
int length = 0;
int lengthImageData = 0;

FileInfo finfo = new FileInfo("a.jpg");
FileStream fStream = File.OpenRead("a.jpg");

byteData = new byte[ finfo.Length];


while ((length = fStream.Read(byteTemp, 0, 512)) != -1) {
for (int i = 0; i < length; i++) {
byteData[lengthImageData + i] = byteTemp[i];
}
lengthImageData += length;}

however if i run it, the console would just hang. i have done some debugging and i suspect something is wrong in my while-loop.

any advice is appreciated.thanks in advance



Re: Visual C# General while loop error--need help

Karthikeya Pavan Kumar .B

My guess is that it goes into a recurssive loop try this

Code Snippet

while ((length = fStream.Read(byteTemp, 0, 512))!=0)






Re: Visual C# General while loop error--need help

frederikm

try

Code Snippet

while (fStream.Peek()!=-1)

Hope this helps you out






Re: Visual C# General while loop error--need help

mwalts

Seems to me the big problem your having is that -1 can never appear. Read returns 0 when it reaches the end of the stream, not -1. As can be seen in the remarks section of MSDN

http://msdn2.microsoft.com/en-us/library/system.io.filestream.read.aspx

Have fun,

-mwalts






Re: Visual C# General while loop error--need help

Jeremy Filburn

Not really sure what you are after here, but maybe this code snippet can help you!

Code Snippet

int numberOfBytes = 512;

int numberOfBytesRead = 0;

byte[] buffer = new byte[numberOfBytes];

FileInfo fi = new FileInfo(@"E:\a.jpg");

using (FileStream fs = fi.OpenRead())

{

for (int i = 0; i < fs.Length / 512 + 1; i++)

{

//Read 512 bytes into an array from the specified file.

numberOfBytesRead = fs.Read(buffer, 0, numberOfBytes);

Console.WriteLine(

"{0} bytes have been read from the specified file.", numberOfBytesRead);

}

}

Console.ReadLine();






Re: Visual C# General while loop error--need help

rookie_rockie

thanks for ur reply .can u explain to me why u r using this: i < fs.Length / 512 + 1




Re: Visual C# General while loop error--need help

Jeremy Filburn

The reason for the 1 extra iteration is to get any bytes left over that do not evenly devide by 512. If the file size was 100000 bytes. 100000 / 512 = 195.3125, so the last pass will get the last 312.5 bytes, hence the + 1.

This means that it will pass through this iteration 195 times, and we need to get the last chunk.

If you were to do any more passes, the number of bytes read would be 0. This for loop will run even if no bytes are read.






Re: Visual C# General while loop error--need help

Jeremy Filburn

You could also replace the for loop with this:

Code Snippet

do

{

//Read 512 bytes into an array from the specified file.

numberOfBytesRead = fs.Read(buffer, 0, numberOfBytes);

Console.WriteLine(

"{0} bytes have been read from the specified file.", numberOfBytesRead);

} while (numberOfBytesRead > 0);






Re: Visual C# General while loop error--need help

rookie_rockie

Can u show me how to store the bytes that have been read into a byte[] and print it out





Re: Visual C# General while loop error--need help

Jeremy Filburn

Storing and printing are 2 different things.

Can I ask you why you would want to store the bytes when you can derrive them from an existing file like the above example






Re: Visual C# General while loop error--need help

rookie_rockie

i would like to derive the bytes from the file that i read and store them in a byte array.the purpose of printing is to check has entire file been read into the stream. this is what i have done :
Code Snippet

public static byte[] image(string fileName)
{

byte[] byteData = null;
byte[] byteTemp = new byte[512];
int length = 0;
int lengthImageData = 0;


FileInfo finfo = new FileInfo(fileName);
FileStream fs = finfo.OpenRead();

byteData = new byte[finfo.Length];


while ((length = fs.Read(byteTemp, 0,512)) != 0)

{
for (int i = 0; i < length; i++)

{
byteData[lengthImageData + i] = byteTemp[i];


}
lengthImageData += length;

}

return byteData;}

pls advise me





Re: Visual C# General while loop error--need help

Jeremy Filburn

Code Snippet

byte[] bytes;
string path = @"C:\Temp\Forest.jpg";
FileInfo fi = new FileInfo(path);

if (File.Exists(path))
{
using (FileStream fs = fi.OpenRead())
{
int numberOfBytesRead = 0;
bytes = new byte[fs.Length];
numberOfBytesRead = fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
Console.WriteLine("{0} bytes have been read.", numberOfBytesRead);
Console.WriteLine("Byte array size = {0}", bytes.Length);
}
}
Console.ReadLine();