开发者

Formatted output with control characters in C?

After reading about control characters from Wikipedia, I have tried to write some code using them. When I write one of them, I could not successfully write the required/wanted output:

name    :  ...
surname :  ...
              asadsa  // these three words are some data 
              adsdsa  
              asdsds

my effort:

fprintf(stdout, 开发者_如何学运维"\rname\t\t\b:%s\r\fsurname\t:%s\v %s \v%s \v%s ",
        name, surname, data1, ...)

but output is:

name     : georg.
surname  : alafron.
                   to the school
                   ....
                   ....

but this output is not desired one, so how can I fix it only using fprintf() and control characters?


In general, you will seldom have cause to use any of the control characters '\a', '\b', '\f', '\v', and you won't often use '\r'; that leaves '\t' and '\n', both of which you probably will use. The last time I used '\r' and '\b', I was writing code to do bold on a daisy wheel printer by overstriking - writing the same data twice. It doesn't help on a modern ink jet or laser printer.

It is a bit difficult to see what the differences between the required and actual outputs are. However, assuming that your data is in variables name, surname, data1, data2, and data3, then this format string should do what you desire:

printf("name    :  %s\nsurname :  %s\n%14s%s\n%14s%s\n%14s%s\n",
       name, surname, "", data1, "", data2, "", data3);

The '%14s' format specifies that the width should be 14 characters, which is the size of the indent on your data lines. The empty string gets blank padded to 14 characters, and is followed by the relevant bit of data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜