Solved

Stream Writer, Buffer

Posted on 2011-03-09
5
438 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

707 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

16 Experts available now in Live!

Get 1:1 Help Now