Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stream Writer, Buffer

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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
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.

610 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