LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

setctrlval

I am trying to print the contents of a file to a textbox using SetCtrlVal. For some reason, I just get a blank textbox. The values in output.txt are correct, so the program is working. I have attached a sample of what I'm trying to do.
0 Kudos
Message 1 of 4
(4,733 Views)
I'm not sure that you can use the file pointer fout as a parameter to SetCtrlVal but if you can, one reason it may not be working is because you close the file before calling SetCtrlVal. If fout doesn't work as a parameter, you can save the contents to a buffer first, then use that as the parameter to SetCtrlVal.
0 Kudos
Message 2 of 4
(4,733 Views)
BJA is right.
fout is a file pointer or stream. The textbox is looking for a string.
Create a character array to hold all the text.
Add each outchar character to the character array.
When you're done reading the file, append '\0' to your character array to make it a null terminated string.
Use your new string in SetCtrlVal, not the file pointer for a closed file.
Make another correction to SetCtrlVal: the first parameter to SetCtrlVal is the panel handle returned by LoadPanel, not the panel constant. See the function panel help or on-line help for SetrCtrlVal. If your new string is called outString, here's the SetCtrlVal call.
SetCtrlVal(panelHandle, PANEL_TEXTBOX, outString);
Why are you casting inchar[0] as a char when you already declared char inchar[2]?

Since you're subtracting 128, it wuould actually be better if you declared inchar and outchar unsigned char.
You may want to do some error checking:
Did fread read a character? Note: using fread and feof the way you do, you call fread one more time after reading the last character in the file.
Is the file size greater than expected (the size of outString)?
Is inchar < 128?
Your code may end up something like this.
#define MY_MAX_FILE_LENGTH 100 // whatever length
unsigned char inchar[2], outchar[2];
char outString[MY_MAX_FILE_LENGTH];
int i;
// fopen stuff
//...
i=0;
while(!feof(FH))
{
if (fread(inchar,1,1,FH) > 0)
{
// a character was read
if (inchar[0] >= 128)
{
outchar[0] = (inchar[0] - 128);
fwrite(outchar,1,1,fout);
if (i < MY_MAX_FILE_LENGTH)
outString[i++] = outchar[0];
else
{
//handle the error
}
}
else
{
//handle the error
}
}
}
outString[i] = '\0';
fclose(FH);
fclos
e(fout);
SetCtrlVal (panelHandle, PANEL_TEXTBOX, outString);
Message 3 of 4
(4,733 Views)
P.S.
I don't know what your VHP file format is, so I just came up with another question. Do you want to substract 128 from inchar or just mask off the most-significant bit (MSB)? If you want to mask the MSB, you can change the statement:
From:
outchar[0] = (inchar[0] - 128);
To:
// bit-wise And to mask off MSB
outchar[0] = inchar[0] & 0x7f;
0 Kudos
Message 4 of 4
(4,733 Views)