• TwitterFacebookGoogle PlusLinkedInRSS FeedEmail

Filestream C

5/22/2019 
P: 7
Hello there, I am dealing with files encoded in UTF8 and I can't find a way to convert them into ANSI.
I've already searched in google for this since a while, and I'm not achieving the result I want to achieve if I use the code I've found on the web, which is the following:
Example of test.txt (save it as UTF8): 'יאטxFCצה'
  1. string filePath = 'c:test.txt';
  2. StreamReader fileStream = new StreamReader(filePath);
  3. string fileContent = fileStream.ReadToEnd();
  4. fileStream.Close();
  5. StreamWriter ansiWriter = new StreamWriter(filePath.Replace('.txt', '-ansi.txt'), false);
  6. ansiWriter.Write(fileContent, Encoding.Default);
  7. ansiWriter.Close();
What I get is as result as c:test-ansi.txt the same file (same size) and same encoding. What I can conclude is that the 'Default' encoding of my machine is UTF8.
So then my question is, can you help me to correct the code snippet above so that the 'ANSI' encoding will be used, instead of the 'Default' one? In Encoding I can find only ASCII, UTF7, UTF16, Unicode and UTF8, I couldn't find the ANSI one..
Thanks in advance, you can't imagine how many tests I did so far..

Input/output stream class to operate on files. Objects of this class maintain a filebuf object as their internal stream buffer, which performs input/output operations on the file they are associated with (if any). File streams are associated with files either on construction, or by calling member open. This is an instantiation of basic_fstream with the following template parameters.

What is the best method to convert a Stream to a FileStream using C#.

The function I am working on has a Stream passed to it containing uploaded data, and I need to be able to perform stream.Read(), stream.Seek() methods which are methods of the FileStream type.

A simple cast does not work, so I'm asking here for help.

GregGreg

1 Answer

Filestream

Read and Seek are methods on the Stream type, not just FileStream. It's just that not every stream supports them. (Personally I prefer using the Position property over calling Seek, but they boil down to the same thing.)

If you would prefer having the data in memory over dumping it to a file, why not just read it all into a MemoryStreamDriver restore legit. ? That supports seeking. For example:

Use:

Boyka undisputed 4 full movie. Injured Yuri asks Alma if she can forgive him for what he did to her husband. Yuri gets shot in his stomach, but he grabbed Zourab, punch in his face and choke him to death.

Filestream.com

This is similar to using the Stream.CopyTo method introduced in .NET 4.

If you actually want to write to the file system, you could do something similar that first writes to the file then rewinds the stream.. but then you'll need to take care of deleting it afterwards, to avoid littering your disk with files.

Jon SkeetJon Skeet

Output Filestream C

Not the answer you're looking for? Browse other questions tagged c#streamfilestream or ask your own question.