?
Solved

Formatting of C Output

Posted on 2012-08-28
5
Medium Priority
?
367 Views
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.
0
Comment
Question by:lostinspace9
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 38341321
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.
http://www.cplusplus.com/reference/clibrary/cstdio/printf/

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

Expert Comment

by:TommySzalapski
ID: 38341338
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);
0
 

Author Comment

by:lostinspace9
ID: 38341403
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.
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 38341496
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.
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/

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

Author Closing Comment

by:lostinspace9
ID: 38345399
Thanks
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Simple Linear Regression

829 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