c++ - Padding with spaces in char array -
I am writing a program to write a head for a specific image format. This image format requires 256 characters as its header before any of the following raw image data. But I have trouble emptying the empty space to create a title of 256 characters.
Below is an abstract of my problem:
four pads [256]; Sprintf (Pad, "Header Info:% s =% f", "Scale", 2.3); Cout & lt; & Lt; Pad & lt; & Lt; "Data here" & lt; & Lt; Endl; The output is:
header information: scale = 2.300000 data here However, the result is expected as I Header information: Scale = 2.300000 data here
Where "data here" appears after 256 characters from the beginning of the file. How do I change the program to pad the blank space in the character array?
Do this:
Cust & lt; & Lt; Setup (256) & lt; & Lt; Left & lt; & Lt; Pad & lt; & Lt; "Data here" & lt; & Lt; Endl; You will see #include & lt; iomanip & gt; may be required. In your "real code" BTW you snprintf to ensure that there is no chance of buffer overflow, assuming that your % s Some logic that is used on runtime (or preferably replace sprintf with stringstream).
Comments
Post a Comment