带有尾随空格的固定长度填充

时间:2015-12-17 23:48:38

标签: printf

如何使用12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: FATAL EXCEPTION: main 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: Process: com.example.ahmed.popularmovies, PID: 12140 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ahmed.popularmovies/com.example.ahmed.popularmovies.MainActivity}: android.view.InflateException: Binary XML file line #22: Error inflating class fragment 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2493) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:176) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:111) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:194) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5576) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class fragment 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:806) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:504) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:414) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.example.ahmed.popularmovies.MainActivity.onCreate(MainActivity.java:19) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6005) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:176)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:111)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:194)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5576)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.example.ahmed.popularmovies.data.DbOpenHelper.getReadableDatabase()' on a null object reference 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.example.ahmed.popularmovies.MainFragment.getDataFromDB(MainFragment.java:194) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.example.ahmed.popularmovies.MainFragment.<init>(MainFragment.java:49) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at java.lang.reflect.Constructor.newInstance(Native Method) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1572) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v4.app.Fragment.instantiate(Fragment.java:423) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v4.app.Fragment.instantiate(Fragment.java:398) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2275) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733) 12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:504)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:414)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:365)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:256)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.example.ahmed.popularmovies.MainActivity.onCreate(MainActivity.java:19)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6005)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2446)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2555)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:176)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:111)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:194)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5576)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)  12-18 01:44:36.915 12140-12140/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750) 

实现带尾随空格的固定长度填充

例如,如何让sprintf输出带有尾随空格的50个字符?

1 个答案:

答案 0 :(得分:3)

我的man 3 printf说:

  

可选的十进制数字字符串,指定最小字段宽度。如果            转换后的值比字段宽度少,它将是            在左侧填充空格(如果左侧调整标志有,则填充右侧)            已经给出了填写字段宽度。

  

` - '负场宽标志;转换后的值是                         左边调整了场边界。除了n转换                         sions,转换后的值用右边填充                         空白,而不是左边的空白或零。一个 -                         如果两者都给出,则覆盖0。

  

字段宽度或精度或两者都可以用星号'*'或表示        星号后跟一个或多个十进制数字和'$'而不是a        数字串。在这种情况下,int参数提供字段宽度或精度。

所以我试过了:

#include <stdio.h>
#include <string.h>

int main() {
    int num = 123;
    char* text = "The number is: ";
    printf("%s%-*iX\n", text, 50 - (int) strlen(text), num);
}

得到:

The number is: 123                                X

(其中X只是一个标记,用于显示填充空格。)