467,915 Members | 1,599 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

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
  • viewed: 1535
Share:
6 Replies
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Donnal Walter | last post: by
10 posts views Thread by Jeff B. | last post: by
reply views Thread by Stuart Woodard | last post: by
4 posts views Thread by Russ | last post: by
3 posts views Thread by Clint MacDonald | last post: by
4 posts views Thread by Al Jones | last post: by
4 posts views Thread by iwdu15 | last post: by
7 posts views Thread by pkirk25 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.