1

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,...
4
  • 1
    What kind of alignment you want and where is your code? Commented Sep 11, 2013 at 5:36
  • 1
    Well maybe you should calculate spaces and offsets when printing to make sure the alignment is okay.... Or you use a format specifier for fprintf to pad the numbers as you wish. Something like %05d should do. Commented Sep 11, 2013 at 5:36
  • 1
    Note that your format string has 11 format specifiers but you are passing 12 values. Commented Sep 11, 2013 at 5:58
  • yup i noted now thanks Commented Sep 11, 2013 at 6:04

1 Answer 1

2

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); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.