fprintf formatted output

M

Magix

Hi,

I want to use fprintf to write to a file. My question about the formatted
output
How can I format so that I can allocate certain width for each %s
(Left-aignlied) ?
Example:
fprintf("%s %s %s %s, name, age, city, status);

<-------------------><----------------><----------------><------------->
Benjamin 50 years old New York
Married
Ken 45 years old Los Angelas
Single

Thanks.
 
R

Ravi Uday

fprintf("%-s %-s %-s %-s, name, age, city, status);
Left alignes the output


See format specifiers of these. Should be available in one of your C
documents.
- Ravi
 
H

Hamed

Hi there,
To show you how it works I give you a code that prints to the standard
output. You can replace stdout with the name of your file. Number 10
indicates the desired width and the negative sign means it's left adjusted.
S is for printing a sequence of chars.
#include <stdio.h>
int main()
{
printf("This is main\n");
fprintf(stdout, "%-10s%-10s\n","Hamed", "Ghorbani" );
}
Sincerely,
Hamed
 
M

Magix

Hamed said:
Hi there,
To show you how it works I give you a code that prints to the standard
output. You can replace stdout with the name of your file. Number 10
indicates the desired width and the negative sign means it's left adjusted.
S is for printing a sequence of chars.
#include <stdio.h>
int main()
{
printf("This is main\n");
fprintf(stdout, "%-10s%-10s\n","Hamed", "Ghorbani" );
}
Sincerely,
Hamed
Thank you. Please don't top-post.
 
K

kal

Magix said:
Hi,

I want to use fprintf to write to a file. My question about
the formatted output
How can I format so that I can allocate certain width for
each %s (Left-aignlied) ?
Example:
fprintf("%s %s %s %s, name, age, city, status);

<-------------><-------------><----------><-------->
Benjamin 50 years old New York Married

Consult your documentation for fprintf.

fprintf(stdout,"%-15.15s%-15.15s%-12.12s%-10.10s\n",
name, age, city, status);

fprintf(stdout,"%-*.*s%-*.*s%-*.*s%-*.*s\n",
15,15,name, 15,15,age, 12,12,city, 10,10,status);
 
O

Olaf

Hello

To get columns use the format

fprintf(stdout,"==%-10.10s %-10.10s==\n","123456789012","1234");

it breaks the to long string on 10 positions

output will be:
==1234567890 1234 ==

the number 10 can be any number, for more details look in every good C
book.
you get all the details by printf. fprintf, sprintf are just the same
only desternation is different.

greetings Olaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top