user319824
user319824

Reputation:

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?

Upvotes: 0

Views: 457

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753870

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.

Upvotes: 2

Related Questions