C >> C# question

  • Thread starter Thread starter Guest
  • Start date Start date
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
 
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));
}
 
Back
Top