write bytes to a file
-
Hi, I worked on a project which writes 2 doc files, in memory stream. From the memory stream I wrote all the bytes to a file something like this:
File.WriteAllBytes(path,mybytes).
The problem is that I can't see all the information. But if I look in my file with Windows Commander the information which I can't see is there. Where should be the problem? Thanks for your help. -
Hi, I worked on a project which writes 2 doc files, in memory stream. From the memory stream I wrote all the bytes to a file something like this:
File.WriteAllBytes(path,mybytes).
The problem is that I can't see all the information. But if I look in my file with Windows Commander the information which I can't see is there. Where should be the problem? Thanks for your help.WhiteGirl23 wrote:
The problem is that I can't see all the information.
"See all the information"? You've gone bind and now can't use a monitor? Your trying to use photoshop to open the doc file but the pixels arn't in the right place? Your highly advanced brain interface isn't causing the right electrical impulses in the visual processing part of your brain? You might want to explain yourself in explicit developer terms rather than clueless user terms. i.e. "I try to look at the value in the watch panel in visual studio" or "if I then open the file in notepad I can't see the byte's I have written". The problem could be that whatever your using to look at the data isn't interpreting it correctly or that the data is being written incorrectly. It's pretty hard to tell without being psychic or being given some pretty basic information.
-
WhiteGirl23 wrote:
The problem is that I can't see all the information.
"See all the information"? You've gone bind and now can't use a monitor? Your trying to use photoshop to open the doc file but the pixels arn't in the right place? Your highly advanced brain interface isn't causing the right electrical impulses in the visual processing part of your brain? You might want to explain yourself in explicit developer terms rather than clueless user terms. i.e. "I try to look at the value in the watch panel in visual studio" or "if I then open the file in notepad I can't see the byte's I have written". The problem could be that whatever your using to look at the data isn't interpreting it correctly or that the data is being written incorrectly. It's pretty hard to tell without being psychic or being given some pretty basic information.
my code is: //write in memory the files
MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1);
//unicodebytes,unicodebytes1 are vectors where I keep the bytes from the files //read the filesBinaryReader binread = new BinaryReader(mem); mem.Position = 0; byte[] byteArray = new byte[mem.Length];
int count; count = mem.Read(byteArray, 0,1); while (count < mem.Length) { byteArray[count++] = Convert.ToByte(mem.ReadByte()); File.WriteAllBytes("C:\\text1.doc", byteArray);
My problem is that I can see the first file in Word but I can't see the second file. -
my code is: //write in memory the files
MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1);
//unicodebytes,unicodebytes1 are vectors where I keep the bytes from the files //read the filesBinaryReader binread = new BinaryReader(mem); mem.Position = 0; byte[] byteArray = new byte[mem.Length];
int count; count = mem.Read(byteArray, 0,1); while (count < mem.Length) { byteArray[count++] = Convert.ToByte(mem.ReadByte()); File.WriteAllBytes("C:\\text1.doc", byteArray);
My problem is that I can see the first file in Word but I can't see the second file.flush the BinaryWriter before re-reading the data.
MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1); **bin.Flush();** // <<<----------------
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
-
my code is: //write in memory the files
MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1);
//unicodebytes,unicodebytes1 are vectors where I keep the bytes from the files //read the filesBinaryReader binread = new BinaryReader(mem); mem.Position = 0; byte[] byteArray = new byte[mem.Length];
int count; count = mem.Read(byteArray, 0,1); while (count < mem.Length) { byteArray[count++] = Convert.ToByte(mem.ReadByte()); File.WriteAllBytes("C:\\text1.doc", byteArray);
My problem is that I can see the first file in Word but I can't see the second file.try to explain the context : what are you trying to achieve ? is it binary data ? text ? in ASCII ? in Unicode ? are they both Word documents ? where does the data originate ? show the declarations ! and specify the symptoms in detail: what does "can see" mean ? is the first file OK, or just recognizable ? and what about the second: nothing at all, some garbage, some parts recognizable, what ? if you are not explicit about these, you either will get no answers, or some answer that is as mysterious as your questions are... :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
flush the BinaryWriter before re-reading the data.
MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1); **bin.Flush();** // <<<----------------
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
thank for your help but still doesn't work
-
try to explain the context : what are you trying to achieve ? is it binary data ? text ? in ASCII ? in Unicode ? are they both Word documents ? where does the data originate ? show the declarations ! and specify the symptoms in detail: what does "can see" mean ? is the first file OK, or just recognizable ? and what about the second: nothing at all, some garbage, some parts recognizable, what ? if you are not explicit about these, you either will get no answers, or some answer that is as mysterious as your questions are... :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
the first file is seen very good. My files are document files. But the second file is missing. I turned my documents files in bytes and these files are written to the memory stream. From the memory stream (where I have the 2 documents in bytes)I want to see the 2 files in one file. Thanks.
-
my code is: //write in memory the files
MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1);
//unicodebytes,unicodebytes1 are vectors where I keep the bytes from the files //read the filesBinaryReader binread = new BinaryReader(mem); mem.Position = 0; byte[] byteArray = new byte[mem.Length];
int count; count = mem.Read(byteArray, 0,1); while (count < mem.Length) { byteArray[count++] = Convert.ToByte(mem.ReadByte()); File.WriteAllBytes("C:\\text1.doc", byteArray);
My problem is that I can see the first file in Word but I can't see the second file.There are a couple of things strange in your code. 1- You assign a BinaryReader but you do not use it. 2- You are writing a bunch of bytes to a MemoryStream. Are you just trying to copy the 2 byte arrays to a single file (if not, what your code is doing totally escapes me)? This is not really needed. Try this instead:
FileStream fs = File.Create(@"C:\text.doc");
fs.Write(unicodebytes, 0, unicodebytes.Length);
fs.Write(unicodebytes1, 0, unicodebytes1.Length);
fs.Flush();
fs.Close();If this is not the case, please explain what you are trying to do.
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
-
There are a couple of things strange in your code. 1- You assign a BinaryReader but you do not use it. 2- You are writing a bunch of bytes to a MemoryStream. Are you just trying to copy the 2 byte arrays to a single file (if not, what your code is doing totally escapes me)? This is not really needed. Try this instead:
FileStream fs = File.Create(@"C:\text.doc");
fs.Write(unicodebytes, 0, unicodebytes.Length);
fs.Write(unicodebytes1, 0, unicodebytes1.Length);
fs.Flush();
fs.Close();If this is not the case, please explain what you are trying to do.
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
thanks for your help but I have the same problem. I can see the first file but I can't see the second. I want to copy 2 documents files in one file.
-
the first file is seen very good. My files are document files. But the second file is missing. I turned my documents files in bytes and these files are written to the memory stream. From the memory stream (where I have the 2 documents in bytes)I want to see the 2 files in one file. Thanks.
Now who claims that appending two document files would result in one document file with all the document content ? You can use a hex dump utility (Visual Studio !), or maybe Wordpad, to check your concatenation succeeded technically, Word has already proven it did not succeed functionaly. You can concatenate two text files, two C# source files (that will result in some compile errors tho), maybe two mp3 files. But you cant get the result you're after when you concatenate two JPEG files, two Visual Studio project files, two MS Word files, etc. You need something smarter and more difficult to achieve that. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
thanks for your help but I have the same problem. I can see the first file but I can't see the second. I want to copy 2 documents files in one file.
Did you try to run it through a debugger? is it possible that unicodebytes1 is empty? Make sure your 2 byte arrays actually contain the bytes.
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
-
my code is: //write in memory the files
MemoryStream mem = new MemoryStream(); BinaryWriter bin = new BinaryWriter(mem); bin.Write(unicodebytes, 0, number); bin.Write(unicodebytes1, 0, number1);
//unicodebytes,unicodebytes1 are vectors where I keep the bytes from the files //read the filesBinaryReader binread = new BinaryReader(mem); mem.Position = 0; byte[] byteArray = new byte[mem.Length];
int count; count = mem.Read(byteArray, 0,1); while (count < mem.Length) { byteArray[count++] = Convert.ToByte(mem.ReadByte()); File.WriteAllBytes("C:\\text1.doc", byteArray);
My problem is that I can see the first file in Word but I can't see the second file.If your trying to combine 2 wrod doc's it's not that simple. Word docs use Structured Storage[^] (aka DocFile[^]) for pre 2007 files. For 2007 there is a new XML format ... you can do your own google for that :P Either way you cannot simply concatenate these documents.
-
Did you try to run it through a debugger? is it possible that unicodebytes1 is empty? Make sure your 2 byte arrays actually contain the bytes.
----- If atheism is a religion, then not collecting stamps is a hobby. -- Unknown
thanks for your help. The size of my new created file is equal with the size of the first file + the size of the second file. The unicodebytes1 is not null. thanks again.
-
thanks for your help. The size of my new created file is equal with the size of the first file + the size of the second file. The unicodebytes1 is not null. thanks again.
-
If your trying to combine 2 wrod doc's it's not that simple. Word docs use Structured Storage[^] (aka DocFile[^]) for pre 2007 files. For 2007 there is a new XML format ... you can do your own google for that :P Either way you cannot simply concatenate these documents.
thanks for all your help. I found how to resolve my problem(with tx textcontrol). Thanks again. Regards, WhiteGirl23