Thread: stream conversion

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    stream conversion

    Hi,

    Can anyone please show me the path to proceed in order to convert memorystream to filestream and vice versa?

    Thanks,
    AK

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    I'm not sure exactly what you mean. This is how I'd do it:
    Code:
    //FileStream to MemoryStream
    MemoryStream ms = new MemoryStream();
    using (FileStream file = new FileStream("in.txt", FileMode.Open))
    {
    	int buffer;
    	while ((buffer = file.ReadByte()) != -1)
    		ms.WriteByte((byte)buffer);
    }
    
    ms.Seek(0, SeekOrigin.Begin);
    
    //MemoryStream to FileStream
    using (FileStream file = new FileStream("out.txt", FileMode.Create))
    {
    	int buffer;
    	while ((buffer = ms.ReadByte()) != -1)
    		file.WriteByte((byte)buffer);
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM