我在使用C
对齐文件时遇到问题程序行:
fprintf(fpscrip,"\n %ld , %ld , %ld , %ld , %ld , %ld , %ld , %ld , %ld , %ld , %ld",scripCode,tradeVolume,LTQ,LTR,OpenRate,CloseRate,HighRate,LowRate,TotBuyQty,TotSellQty,LowerCircuitLimit,UpperCircuitLimit)
file o / p
524667 , 7 , 1 , 34010 , 34500 , 34825 , 34500 , 34010 , 728 , 698 , 27865
533573 , 83625 , 50 , 14260 , 13655 , 13595 , 14440 , 13575 , 9202 , 15989 , 10880
结果应打印如下: -
524667 , 7 , 1 ,...
533573 ,83625,50,...
答案 0 :(得分:2)
与printf()
一样,您需要使用一些左右对齐
printf()
将输出写入stdout
fprintf()
将输出写入给定的输出流;
printf("%6d",num);
// if num have 3 digits then adds three more spaces at left of num.
这样只需要使用fprintf()
在您的文件中,最多只有6位数字。
将所有%ld
替换为%7ld
并在每3或4个数字后添加\ n以获得完美的对齐
fprintf(fpscrip,"\n %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld ",scripCode,tradeVolume,LTQ,LTR,OpenRate,CloseRate,HighRate,LowRate,TotBuyQty,TotSellQty,LowerCircuitLimit,UpperCircuitLimit);