Question

i have problem in aligning the file using C

program line :

    fprintf(fpscrip,"\n %ld , %ld , %ld , %ld , %ld , %ld , %ld , %ld , %ld , %ld , %ld",scripCode,tradeVolume,LTQ,LTR,OpenRate,CloseRate,HighRate,LowRate,TotBuyQty,To‌​tSellQty,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 

The result should print like :-

524667 ,  7  , 1 ,... 
533573 ,83625,50,...
Was it helpful?

Solution

As like printf() You need to use some left and right alignments

printf() write output to stdout

fprintf() write output to the given output stream;

printf("%6d",num); // if num have 3 digits then adds three more spaces at left of num.

like this only you need to use with fprintf()

In your file you have only 6 digits in the maximum number.

replace all %ld with %7ld and add \n after every 3 or 4 numbers to get perfect allignment

fprintf(fpscrip,"\n %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld , %7ld ",scripCode,tradeVolume,LTQ,LTR,OpenRate,CloseRate,HighRate,LowRate,TotBuyQty,To‌​tSellQty,LowerCircuitLimit,UpperCircuitLimit); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top