LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with using SPrintf function in a cpp file

SPrintf can be used with .c extension, but when it's used in a .cpp then you get the following:
-------------
Error C2664: 'SPrintf' : cannot convert parameter 1 from 'char [10]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
------------------
But it does compile with .c extension, however this time the rest of the code doesn't work.

Any ideas?

Thanks,

Sacit
0 Kudos
Message 1 of 6
(4,490 Views)
sprintf can be used in C++ the same way as in C.

This works just fine in C++:

int i=10;
char szBytes[80];
sprintf(szBytes, "%d", i);

Remember that C and C++ are case sensitive. I don't know what 'SPrintf' is, but sprintf is part of any standard c or c++ library (in stdio.h).

Two suggestions on this post:
1. Post to the right group. This is a LabView discussion group, not a C++ group.
2. Show the important lines of code. In this case, the sprintf statement and the declarations of the variables referenced there. Without them, we can't tell you anything more. I can only assume that you have a simple coding error.
0 Kudos
Message 2 of 6
(4,490 Views)
You know, there is a SPrintf that is part of the CIN
interface. To answer this question one needs to look
in extcode.h and see that SPrintf is
TH_REENTRANT int32 SPrintf(CStr, CStr, ...);
where
typedef uChar *PStr, **PStrHandle, *CStr;

From this it seems pretty clear what the problem is, given
these declarations. You can't use SPrintf like the c version
sprintf. You have to get your types right.

Cheers,
Kevin

Al S wrote:

> sprintf can be used in C++ the same way as in C.
>
> This works just fine in C++:
>
> int i=10;
> char szBytes[80];
> sprintf(szBytes, "%d", i);
>
> Remember that C and C++ are case sensitive. I don't know what
> 'SPrintf' is, but sprintf is part of any standard c or c++ library (in
> std
io.h).
>
> Two suggestions on this post:
> 1. Post to the right group. This is a LabView discussion group, not a
> C++ group.
> 2. Show the important lines of code. In this case, the sprintf
> statement and the declarations of the variables referenced there.
> Without them, we can't tell you anything more. I can only assume that
> you have a simple coding error.

--
Kevin A. Brown | CAD Accel. Physics
kbrown@bnl.gov | (01) 631-344-4409
0 Kudos
Message 3 of 6
(4,490 Views)
This is not the "sprintf" function defined in standard C and C++ libraries. SPrintf is a data manager function in Code Interface Node (CIN) libraries.
0 Kudos
Message 4 of 6
(4,490 Views)
Kevin,

Here is a piece from the code:

--------------------------------------------------
CIN MgErr CINRun(LStrHandle ParameterFilePath, LStrHandle DataFilePath, LStrHandle OutputFilePath, int32 replace, int32 ver, int16 calrange, int16 istof, LStrHandle OutputString)
{
int i, StartCh, ChannelNum;
double Density, a, b, sigmachn, sigmawin, sigmascattp, EInit, angle, ns, es,
concentration, totalweight, conversion, Dsample, Count[2048], NetCnts;
char temp[15], Z1Name[2], Z2Name[10][2];

char parafilename;
//char *datafilename = "";
//char *outfilename = "";
//char *cMessageText;

// Make necessary conversions.
LStrLen (*OutputString) = LStrLen(*ParameterFilePath);
//MoveBlock(LStrBuf(*ParameterFilePath), LStrB
uf(*OutputString), LStrLen(*ParameterFilePath));

MoveBlock(LStrBuf(*ParameterFilePath), & parafilename, LStrLen(*ParameterFilePath));

MoveBlock(& parafilename, LStrBuf(*OutputString), LStrLen(*ParameterFilePath));

ifstream parafile, infile;
ofstream outfile;

Ion_Target mIon_Target;
mIon_Target.Z2 = new int(10);
mIon_Target.M2 = new double(10);
mIon_Target.F = new double(10);

// Open parameter file...
parafile.open((char *)parafilename, ios::in|ios::nocreate);
if(parafile == NULL)
{
//cMessageText = "Couldn't Open Parameter File. Maybe it doesn't exist";
// return -1;
return fIOErr;
}

parafile.close();
----------------------------------------------------

The problem is I have to use ifstream method, because the rest of the code depends on it, and I don't want to deal with the conversion. Otherwise, I would have used FMOpen/FMClose utilities.

So the problem is obvious as you see, all I need is to convert LStrHandle to a char.


When I run the code from LabVIEW, "The instruction at "0x77f83e91" referenced at "0x555c3a44". The memory could not be "read"." error message appears.

I'd appreciate your help.

Sacit
0 Kudos
Message 5 of 6
(4,490 Views)
>
> // Make necessary conversions.
> LStrLen (*OutputString) = LStrLen(*ParameterFilePath);
> //MoveBlock(LStrBuf(*ParameterFilePath),
> LStrBuf(*OutputString), LStrLen(*ParameterFilePath));
>
> MoveBlock(LStrBuf(*ParameterFilePath), & parafilename,
> LStrLen(*ParameterFilePath));
>
> MoveBlock(& parafilename, LStrBuf(*OutputString),
> LStrLen(*ParameterFilePath));
>
> ----------------------------------------------------
> So the problem is obvious as you see, all I need is to convert
> LStrHandle to a char.
>
> When I run the code from LabVIEW, "The instruction at "0x77f83e91"
> referenced at "0x555c3a44". The memory could not be "read"." error
> message appears.
>

The above code is writing to uninitialized memory beca
use the
OutputString has not been sized. You have a couple choices here. Add a
line to resize the string before you set the length or do the MoveBlock
to it. I suspect there are multiple ways to resize it, but I don't have
the manuals in front of me.

The second, and easier option is to pass the output string in with it
presized on the diagram. Make sure it is an in/out parameter and pass
the string in that is large enough for you to write on.

As for casting the char and uchar type, use a C style cast such as c=
(char*)LStrBuf(*ParameterFilePath). I suspect that the C file I/O is
expecting a null terminated string, so be sure to take care of that.

Which brings me to the next suggestion. You might want to investigate
moving this over to using the Call Library Function node to call a DLL
rather than using a CIN. They are pretty much equivalent, but the DLL
node will let you directly pass in C strings and it is a bit more
flexible in most uses and easier to get to com
pile.

Greg McKaskle
0 Kudos
Message 6 of 6
(4,490 Views)