LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

A function to format strings or variables

Solved!
Go to solution

Hi,

 

Is there a function to space out strings and etc. without impeding on a set character length. i.e. I want to spread out my table columns.

I'm currently using a lot of spaces to format it. The tab escape doesn't seem to have an effect on moving it at all.

 

Any help will be appreciated.

0 Kudos
Message 1 of 10
(3,623 Views)

 Hi mpwbs,

 

Have you tried using the Table Control Attributes for the size of your table? 

http://zone.ni.com/reference/en-XX/help/370051V-01/cvi/uiref/cvitable_attributes/

 

If these attributes don't work for you, can you elaborate on what you mean by spreading out your table columns? Do you need the width of your columns to be a set size that is bigger than the set number of characters that can be entered?

Adena L.
Technical Support Engineer
National Instruments
0 Kudos
Message 2 of 10
(3,597 Views)

Well, I'm not creating a table. I'm creating one by hardcoding. So, there is a header and then the data follows below.

If the one section has a character of 3 or more then all my alignment is thrown out the window. Wondered if there was a command or something that can set how far I am from the previous character. Like in C++, there is setw(). I wonder if there is something other than "%5d" so it moves it 5 spaces or spots over that would work. But, then I would need to sprintf all my strings and character which can't be done because they are fixed strings/arrays. Attached is the example:

 

0 Kudos
Message 3 of 10
(3,588 Views)

I know two ways:

  1. use a non-proportional font like Courier New and sprintf your content, this way all the columns will stay below each other
  2. use a List Box Control and the formating codes available there, see InsertListItem
-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 4 of 10
(3,573 Views)

Just to emphasize what CVI-User says: find a reference on the printf family of functions (fprintf, sprintf etc) and fully understand all the options the standard c library gives you there.

0 Kudos
Message 5 of 10
(3,505 Views)
Solution
Accepted by topic author mpwbs

@mpwbs wrote:

Well, I'm not creating a table. I'm creating one by hardcoding. So, there is a header and then the data follows below.

If the one section has a character of 3 or more then all my alignment is thrown out the window. Wondered if there was a command or something that can set how far I am from the previous character. Like in C++, there is setw(). I wonder if there is something other than "%5d" so it moves it 5 spaces or spots over that would work. But, then I would need to sprintf all my strings and character which can't be done because they are fixed strings/arrays. Attached is the example:

 


I did something like this for a logging function.  This function outputs columnar formatted rows of text that are then written to file, no a MessagePopup.  But outputting to a popup should work the same.


The '-' character will left-justify the substring.

#define TIME_COL 10
sprintf(string, "%-*s", TIME_COL, TimeStr());

2019-01-02 14_56_19.png

 

Message 6 of 10
(3,491 Views)

I'll give that a try. I'll let you know how it goes. 

 

Thanks!

0 Kudos
Message 7 of 10
(3,487 Views)

@ElectroLund how do I format a character to a character variable like you did in your output below. I get a mismatch error when I try to do it. And if I try using a string as the character mapping. It won't move the output at all. Even if I put 100 spaces there.

0 Kudos
Message 8 of 10
(3,482 Views)

I'm not totally clear what you are seeing.  But I'm guessing you got a GPF for specifiers not matching?  In my example above, "string" is a character buffer

 

char string[100] = {0};

Now make sure that the elements correspond correctly.  When you use a '*' character in place of a literal width, you can then use a variable reference for width.  This is not required, you could have "hard-coded" the width.  But if you use a variable width, you must have the order correct in the specifier list!

 

2019-01-03 09_25_22-Solved_ Re_ A function to format strings or variables - Discussion Forums - Nati.png

 

This is a great reference.

Message 9 of 10
(3,473 Views)

Okay, I sorta found out why I was running into issues. It was a dumb mistake on my part. Never changed my variables after I added spaces into them. But I do understand what you were saying in the last reply. 

 

Thanks for the help!

0 Kudos
Message 10 of 10
(3,468 Views)