简体   繁体   English

如何格式化 c 中 printf 的输出

[英]How to format outputs from printf in c

Im new to C and trying to format a string that correlates to the spacing of another.我是 C 的新手,并尝试格式化与另一个字符串的间距相关的字符串。

I am aiming for the example output below:我的目标是下面的示例 output:

Orders for Pizzeria Freddy's
#   Customer                  Pizza               Price     Time      
------------------------------------------------------------------------
01 >Fred                      Hawaiian            $15.99    15

To do this i made two functions:为此,我做了两个功能:

void print_header(struct pizzeria *the_pizzeria) {
    printf("Orders for Pizzeria %s\n", the_pizzeria->name);
    printf("#   Customer                  Pizza               Price     Time\n");
    printf("------------------------------------------------------------------------\n");

}

and

void print_order(struct order *the_order, int order_number, bool selected) {

    if (selected == true){
        printf("%02d >%4s %20s $%0.2f %20s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time);
    }

    else{
        printf("%02d  %4s %20s $%0.2f %20s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time);
    }

}

I have also tried using %20s to format the string in function print_order but got errors and not the intended output:我还尝试使用%20s格式化 function print_order中的字符串,但出现错误,而不是预期的 output:

void print_order(struct order *the_order, int order_number, bool selected) {
    if (selected == true){
        printf(("%02d >%4s" + String.format("%20s", the_order->pizza) + "$%0.2f" + String.format("%20s\n", the_order->time)), order_number, the_order->customer the_order->cost);
    }

    else{
        printf(("%02d >%4s" + String.format("%20s", the_order->pizza) + "$%0.2f" + String.format("%20s\n", the_order->time)), order_number, the_order->customer the_order->cost);
    }

}

It will add spaces at the end to它将在末尾添加空格

int printPad(int len, const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    int plen = vprintf(fmt, args);
    for(unsigned pad = 0; pad < len - plen; pad ++) putc(' ', stdout);
    va_end(args);
    return len;
}

and print field by field (one field at each call)并逐个字段打印(每次调用一个字段)

despite not mentioning a lot of information like your main code or the datatypes inside struct order and struct pizzeria , but also there are many things to keep in mind when talking about C, there is no bool data type in C, instead it's a type defined value in header called #include<stdbool.h> , but it's not a primitive datatype in C, also values like true and false are hash defined values in same header to values 1 and 0 respectively, for the problem of padding, I liked the above answer made by 0___________ , but the solution I posted below is just using trail and error to find perfect padding suitable for you, also I added the missing information code blocks like main, etc, here is my solution:尽管没有提到很多信息,例如您的主要代码或struct orderstruct pizzeria中的数据类型,但是在谈论 C 时要记住很多事情,C 中没有bool数据类型,而是定义的类型value in header called #include<stdbool.h> , but it's not a primitive datatype in C, also values like true and false are hash defined values in same header to values 1 and 0 respectively, for the problem of padding, I liked the以上答案由0___________ 提出,但我在下面发布的解决方案只是使用跟踪和错误来找到适合您的完美填充,我还添加了缺少的信息代码块,如 main 等,这是我的解决方案:

#include <stdio.h>
#include <stdint.h>

typedef uint8_t bool;
#define true    1
#define false   0

struct pizzeria{
    char *name;
};

struct order{
    char *customer;
    char *pizza;
    float cost;
    char *time;
};

void print_header(struct pizzeria *the_pizzeria) {
    printf("Orders for Pizzeria %s\n", the_pizzeria->name);
    printf("#   Customer    Pizza       Price   Time\n");
    printf("------------------------------------------------------------------------\n"); 
 

}

void print_order(struct order *the_order, int order_number, bool selected) {

    if (selected == true){
        printf("%02d >%s %11s    $%0.2f %3s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time);
    }

    else{
       printf("%02d >%s %11s    $%0.2f %3s\n", order_number, the_order->customer, the_order->pizza, the_order->cost, the_order->time);
    }

}

int main(){
    struct pizzeria Freddy = {"Freddy's"};
    struct order theOrder = {
            .cost = 15.99f,
            .time = "15",
            .customer = "Freddy's",
            .pizza = "Hawaiian"
    };

    print_header(&Freddy);
    print_order(&theOrder, 1, true);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM