Solved

Stream Writer, Buffer

Posted on 2011-03-09
5
444 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

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 86

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

733 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