?
Solved

Stream Writer, Buffer

Posted on 2011-03-09
5
Medium Priority
?
447 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 500 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 500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses

765 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