Printing formatted numbers into text file

P

Prime Mover

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.
 
G

G.S.

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/library/system.io.filestream.aspx
 
M

maximz2005

Thank you so much. That worked perfectly.

Emil

using (TextWriter tw = new StreamWriter("output.txt"))
{
         tw.WriteLine("{0:###.###} {1:###.###} {2:###.###}", x, y,
z);

}  // output stream is automatically closed here
I believe that's one way to do it.


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, y, z));

} //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,
y, z));
} //end for

//Write to the file...

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

str = null;
 
A

Arne Vajhøj

maximz2005 said:
using (TextWriter tw = new StreamWriter("output.txt"))
{
...
tw.WriteLine("{0:###.###} {1:###.###} {2:###.###}", x, y,
z);
...
} // 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, y, z));

} //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,
y, z));
} //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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top