Formatting of C Output

Posted on 2012-08-28
Last Modified: 2012-08-29
I am trying to format the output of some C code and need to do something like the following incomplete VB snippet:

Val(Mid(.Text, 31,12))  
Val(Mid(.Text, 43,12))  
Val(Mid(.Text, 55,12))

As you can see I want to format the columns to be a width of 12 regardless of the value written to it.  The first column is a date & time column which I am not concerned about and then there are 11 formatted columns of other data.
Question by:lostinspace9
    LVL 37

    Expert Comment

    printf lets you specify a width. So printf("%s12", string_var") should print the string_var and pad it to a width of 12. You can also use %s* and pass the width as a parameter.

    If it's C++, then you also could use iomanip with setw.
    LVL 37

    Expert Comment

    Or if you are trying to read in the values, then use strncpy.
    strncpy(dest, source, 12);
    strncpy(dest2, source+12, 12);
    strncpy(dest3, source+24, 12);

    Assuming all are character arrays.
    source+12 gives you source starting at the 13th character.

    If they are std::string types then all you need is the substring function.
    dest3 = source.substr(24, 12);

    Author Comment

    I know that I referenced text in my question but I should have explained that the columns will contain integers and floats.  Thanks for the answer though and I will use it in the future when I actually deal with strings.
    LVL 37

    Accepted Solution

    The printf stuff should work with integers also if you are trying to output it.

    If you are reading in data, then how is it coming? Do you have a string with the data in it that you are trying to split? If so, then it will work, you just need one more step.

    value_var = atoi(dest3); //this will pull the integer values out.

    If you are reading it directly from a file, then you probably could use scanf. It's like printf, but for input.

    If that still isn't helping, then please explain more precisely what it is you want to do.

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 67
    sumHeights  challenge 17 50
    count7 challenge 12 52
    countHi challenge 25 57
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now