So I know how to use Streams in text files, but is there a way to use streams for files other than text? (I tried to use streams in a Word File, but it turned out to be corrupt.)
Asked
Active
Viewed 361 times
-1
-
See [this](https://stackoverflow.com/questions/38356903/how-to-write-text-into-a-word-file-using-c-net) for how you can write to Word files. `StreamReader`/`StreamWriter` reads/writes _plain text_. Word files is not plain text. – Sweeper May 27 '21 at 05:55
-
That's because Word file - is binary file. Try to open it in Notepad++ for example, and you'll see that it is not readable. – kosist May 27 '21 at 05:55
-
MSWord `.docx` are a ZIPs having XMLs and other files: can be renamed and browsed, or opened as-is using a C# component. But old `.doc` are raw binaries and proprietary format. There is also for example `RTF` files, another format. What do you need? – May 27 '21 at 05:58
-
.txt is a file extension, commonly used in conjunction with text files. If the file isn't a text file, then you'll have to use (or create something) that reads the file format you're dealing with according to it's specification. – ProgrammingLlama May 27 '21 at 05:59
-
1"So I know how to use Streams in text files, but is there a way to use streams for files other than text?" - that sentence basically says "I have no idea what I'm doing with streams" - streams are *binary* pumps; it is TextReader/TextWriter that act as a bridge for text, and the specific TextReader/TextWriter that talk to streams are: StreamReader/StreamWriter. If you want to look at a binary file: Stream is fine - but that won't help you read a Word document, unless you also have a library that knows how to understand Word documents! – Marc Gravell May 27 '21 at 07:28
1 Answers
0
Streams work with binary data. Your problem is most likely related to how you send data to the stream, not with the stream itself.
The StreamWriter
class is specialized in converting strings to a binary data, and then sending them to a Stream
.
If you want to write binary data to a stream, you can use the Stream interface itself. There's no need to use a Writer.

Fábio Batista
- 25,002
- 3
- 56
- 68