C >> C# question

G

Guest

I create a lot of text files in an app I am converting to C#. In C, I could
output text to a file in the format:

int i=4;
int j=3;
int k=10;
int m=15;
fprintf(fp,"%4d %4d\n",i,j;
fprintf(fp,"%4d %4d\n",k,m;

and the output would be:

4 3
10 15

In this way, I can stuff spaces into the unneeded characters so that my
columns always lined up in a right-aligned manner.

I can't seem to duplicate this in C#, the closest I found to duplicating
this is to put,

i.ToString("0000")+" "+j.ToString("0000");

to get:

0003 0004

However, I don't want leading zeros, I want leading spaces.

Any ideas?

Thanks,

Phil
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Phil,

The same formating in C# you'll get using

int i=4;
int j=3;
int k=10;
int m=15;
using (StreamWriter sw = new StreamWriter("TestFile.txt"))
{
sw.WriteLine(String.Format("{0,4}{1,4}", i, j));
sw.WriteLine(String.Format("{0,4}{1,4}", k, m));
}
 

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