473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 1701
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.