dongliang1223 2015-12-04 16:23
浏览 43
已采纳

来自vsprintf的奇怪输出

I am trying to build a data-file with hardcoded strings...

So I am using this format:

Format: %a1s%06c%a20s%a30s%a30s%a30s%a30s%a7s%a13s%a20s%a8s%a1s%a1s%a1s%a30s%a14s%a1s%a2s%a39s%a8s%a30s%a2s%a13s%a5s%a7s%a1s%a8s%03c
Values:     "A",    "16",   "",     "",     "Ola Nordmann",     "Slottsgate 1",     "",     "1012",     "",     "",     "",     "",     "",     "P",    "",     "",     "I",    "",     "",     "",     "",     "",     "",     "",     "",     "",     "",     ""

Output: "1s20s30s30s30s30s7s13s20s8s1s1s1s30s14s1s2s39s8s30s2s13s5s7s1s8s"

My code:

    $aRecord = vsprintf($format, $values);
    return ['a-record' => $aRecord, 'values' => $values, 'format' => $format];

Probably just beacuse its pretty late and friday afternoon, hope some of you guys will se my mistake... Thanks! :)

EDIT:

Expected output:

A000016                                                  Ola Nordmann            Slottsgate 1                                                1012
  • 写回答

1条回答 默认 最新

  • doudui9516 2015-12-05 23:10
    关注

    So the solution whas pretty simple :P

    For left-aligning text, we need to use - instead of a

    printf("% -20s", "Richard");
    

    Hope this helps someone :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂