472,961 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,961 software developers and data experts.

Printing formatted numbers into text file


In C, if I have three variables obtained in a loop like:

for(i=0;i<N;i++)
{
x[j] = ....{expression here};
y[j] = ....{expression here};
z[j] = ....{expression here};

}

And I need to print them in lines in a text file for each j, so that I
have
a text file with N lines in the end of the procces, I should make:

FILE *out;

out = fopen("output.txt","a"); // option a is for appending at the end
of the file

for(i=1;i<N;i++)
{
x[j] = ....{expression here};
y[j] = ....{expression here};
z[j] = ....{expression here};

fprintf(out,"%3.f\t%3.f\t%.3f\n",x[j],y[j],z[j]);
// I want float numbers printed with 3 precision digits...\t
prints a tabulation and \n prints a new line

}

fclose(out);

What should I do in C# do get the same result???

Thank you very much.
Aug 22 '08 #1
6 1686
On Aug 22, 10:05*am, Prime Mover <eple...@hotmail.comwrote:
In C, if I have three variables obtained in a loop like:

for(i=0;i<N;i++)
{
* * x[j] = ....{expression here};
* * y[j] = ....{expression here};
* * z[j] = ....{expression here};

}

And I need to print them in lines in a text file for each j, so that I
have
a text file with N lines in the end of the procces, I should make:

FILE *out;

out = fopen("output.txt","a"); // option a is for appending at the end
of the file

for(i=1;i<N;i++)
{
* * x[j] = ....{expression here};
* * y[j] = ....{expression here};
* * z[j] = ....{expression here};

* * fprintf(out,"%3.f\t%3.f\t%.3f\n",x[j],y[j],z[j]);
* * // I want float numbers printed with 3 precision digits...\t
prints a tabulation and \n prints a new line

}

fclose(out);

What should I do in C# do get the same result???

Thank you very much.
I can only point you, not provide working code - sorry...

To format the strings, you'd probably need one of the String.Format()
overloads:
http://msdn.microsoft.com/en-us/library/1ksz8yb7.aspx

Look at System.IO (FileStream) for writing into a file:
http://msdn.microsoft.com/en-us/libr...ilestream.aspx
Aug 22 '08 #2
MC
using (TextWriter tw = new StreamWriter("output.txt"))
{

...

tw.WriteLine("{0:###.###} {1:###.###} {2:###.###}", x[i], y[i],
z[i]);

...

} // output stream is automatically closed here

I believe that's one way to do it.
--
Michael A. Covington, Associate Director
Institute for Artificial Intelligence
The University of Georgia, Athens, GA 30602-7415
www.ai.uga.edu/mc

Aug 22 '08 #3
Thank you so much. That worked perfectly.

Emil

On 22 ago, 12:48, "MC" <for.address.l...@www.ai.uga.edu.slash.mc>
wrote:
using (TextWriter tw = new StreamWriter("output.txt"))
{

* *...

* * * * *tw.WriteLine("{0:###.###} {1:###.###} {2:###.###}", x[i], y[i],
z[i]);

* *...

} *// output stream is automatically closed here

I believe that's one way to do it.
--
Michael A. Covington, Associate Director
Institute for Artificial Intelligence
The University of Georgia, Athens, GA 30602-7415www.ai.uga.edu/mc
Aug 22 '08 #4
On Aug 22, 11:24*am, Prime Mover <eple...@hotmail.comwrote:
Thank you so much. That worked perfectly.

Emil

On 22 ago, 12:48, "MC" <for.address.l...@www.ai.uga.edu.slash.mc>
wrote:
using (TextWriter tw = new StreamWriter("output.txt"))
{
* *...
* * * * *tw.WriteLine("{0:###.###} {1:###.###} {2:###.###}", x[i], y[i],
z[i]);
* *...
} *// output stream is automatically closed here
I believe that's one way to do it.
--
Michael A. Covington, Associate Director
Institute for Artificial Intelligence
The University of Georgia, Athens, GA 30602-7415www.ai.uga.edu/mc
You can use:

using (StreamWriter sw = new StreamWriter("C:\output.txt"))
{

for(int i =0; i < n; i++)
{

//Format and write...
sw.WriteLine(string.Format("{0:###.###} {1:###.###}
{2:###.###}", x[i], y[i], z[i]));

} //end for

} //end using

----------------------
Although, I think the below way would be better, because the above way
has a constantly open connection to the file while your program
formats the strings and then writes the line.

----------------------

StringBuilder str = new StringBuilder();
for(int i = 0; i < n; i++)
{
//Format the string
str.Append(string.Format("{0:###.###} {1:###.###} {2:###.###}", x[i],
y[i], z[i]));
} //end for

//Write to the file...

using (StreamWriter sw = new StreamWriter("C:\output.txt"))
{
sw.WriteLine(str.ToString());
}

str = null;
Aug 23 '08 #5
maximz2005 wrote:
On Aug 22, 11:24 am, Prime Mover <eple...@hotmail.comwrote:
>On 22 ago, 12:48, "MC" <for.address.l...@www.ai.uga.edu.slash.mc>
wrote:
>>using (TextWriter tw = new StreamWriter("output.txt"))
{
...
tw.WriteLine("{0:###.###} {1:###.###} {2:###.###}", x[i], y[i],
z[i]);
...
} // output stream is automatically closed here
I believe that's one way to do it.
Thank you so much. That worked perfectly.

You can use:

using (StreamWriter sw = new StreamWriter("C:\output.txt"))
{

for(int i =0; i < n; i++)
{

//Format and write...
sw.WriteLine(string.Format("{0:###.###} {1:###.###}
{2:###.###}", x[i], y[i], z[i]));

} //end for

} //end using

----------------------
Although, I think the below way would be better, because the above way
has a constantly open connection to the file while your program
formats the strings and then writes the line.

----------------------

StringBuilder str = new StringBuilder();
for(int i = 0; i < n; i++)
{
//Format the string
str.Append(string.Format("{0:###.###} {1:###.###} {2:###.###}", x[i],
y[i], z[i]));
} //end for

//Write to the file...

using (StreamWriter sw = new StreamWriter("C:\output.txt"))
{
sw.WriteLine(str.ToString());
}

str = null;
Keeping a file open is not a problem like keeping a database connection
open.

And by writing directly to the file you avoid potential problems
like forgetting line delimiters.

Arne
Aug 23 '08 #6
On Fri, 22 Aug 2008 20:32:31 -0700, maximz2005 <ma*******@gmail.comwrote:
[...]
Although, I think the below way would be better, because the above way
has a constantly open connection to the file while your program
formats the strings and then writes the line.
That's not necessarily a bad thing. In any case, using a StringBuilder
could well be prohibitive if the output file is of significant length.

Pete
Aug 23 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Donnal Walter | last post by:
We want to be able to print HTML or PDF files to a given printer from Python in a kind of batch mode (without opening a preview window or printer dialog for each file). The printer is on a network,...
10
by: Jeff B. | last post by:
Has anyone come across a decent algorithm for implementing word wrap features in .net printing? I have a small component that uses basic printing techniques (i.e. e.Graphics.DrawString in a...
0
by: Stuart Woodard | last post by:
I am struggling to develop a robust mechanism for server-side printing with formatted documents. I need to produce print-spool files from the remote server-side application. This isn't...
4
by: Russ | last post by:
To ASP.NET printing experts: My Asp.net web form needs to print some reports at the client side. I've been trying to research this and find some confusing and conflicting information in previous...
3
by: Clint MacDonald | last post by:
Hello, I am trying to create a small application that creates labels and I have purchased a thermal transfer label printer. Now I need to be able to print the formatted label to the printer...
4
by: Al Jones | last post by:
Well, hopefully that title get someones attention. In this old gwbasic program I've converted I *have* sucessuflly converted it to print to a file and the output is acceptable. From there I have...
4
by: iwdu15 | last post by:
can anybody help me print from a rich text box? i tried the way they showed on the MSDN web page, but it did not work. I am using VB.net 2003...any help would be appreciated
7
by: pkirk25 | last post by:
My data is in a big file that I have no control over. Sometimes its over 30 MB and often there are several of them. It is machine generated and is nicely formatted. Example text follows: ...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.