Solved

Stream Writer, Buffer

Posted on 2011-03-09
5
439 Views
Last Modified: 2013-12-17
Experts,

I need some direction in writing files. All I`m trying to do is write some lines [a list of doubles] to a file [the exact number could be from a 10 to a few thousands].

Example :
12.55 45 67.5465
11.56 87 87.9876
09.45 65 12.1234
.
.
.
.
11.56 87 87.9876

Is it a good idea to loop through the lines and do a StreamWriter - WriteLine() or should I use Buffers, Send all the info to a buffer and write it once. How do I go about doing that?

A simple example should be awesome!

0
Comment
Question by:San24
  • 2
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35088452
How is your data currently being stored?  (what is the datatype of the structure?)
0
 

Author Comment

by:San24
ID: 35088595
I have the data in a group of Lists. Different points in the files have list of different types. In the example below I have A objects. But in the application I have A, B and C object - always in a sequential manner.

So first A objects need to be written, then B and C and so on.

Also is to format the values - Can I just use ToString("#0.000000") ? Or is there a better way to do it..When I say better, faster.
Class A
{
double X;
double Y;
double Z;
}

//AList - List of A objects
//SWFile is a StreamWriter

 for (int i = 0; i < Cnt; i++)
            {
                SWFile.WriteLine("M  " + AList[i].X + "\t" + AList[i].Y  + "\t" +  AList[i].Z);
            }

Open in new window

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 125 total points
ID: 35088690
Do not worry about using buffers as StreamWriter is pretty efficient.

An alternative to writing the values is as follows:

SWFile.WriteLine("M  {0:#0.000000}\t{1:#0.000000}\t{2:#0.000000}", AList[i].X, AList[i].Y, AList[i].Z);

Open in new window

0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
ID: 35088706
I would override ToString() in each your classes:
public class A
    {

        public double X;
        public double Y;
        public double Z;

        public override string ToString()
        {
            return "M  " + this.X.ToString("#0.000000") + "\t" + this.Y.ToString("#0.000000") + "\t" + this.Z.ToString("#0.000000");
        }

    }

Open in new window


Then you can just use:
SWFile.WriteLine(AList[i].ToString());

Open in new window

0
 

Author Closing Comment

by:San24
ID: 35095268
Thanks guys. Solution reached - StreamWriter the way to go.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now