Highlighted
Absent Member.
Absent Member.
816 views

Returning char* from C routines in Visual COBOL under Unix

Jump to solution

[Migrated content. Thread originally posted on 03 February 2012]

Hello,

I found that when I perform two calls to a C program, and the second string returned by C is smaller than the first one, COBOL does not recognize the 0x0 character and shows the two strings overlapped.

For example:

The first call returns 'file11111.txt', and the second 'file2.txt'. Then COBOL understands 'file2.txt txt'.

Dump of p_szFile in C program ='file11111.txt'                                 
66 69 6C 65 31 31 31 31 31 2E 74 78 74                                         
FILE-W IN COBOL=file11111.txt

Dump of p_szFile in C program ='file2.txt'                                     
66 69 6C 65 32 2E 74 78 74                                                     
FILE-W IN COBOL=file2.txt txt
0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Returning char* from C routines in Visual COBOL under Unix

Jump to solution
This is to be expected as a NULL terminated string is a C construct and not a COBOL one.

In COBOL a PIC X data item can contain any character including a X"00" (NULL).

In order to get the expected results in COBOL you should either initialize the field to spaces before calling the C program or extract the data from the field after the call using something like:


    unstring p_szFile delimited by x"00"
       into p_szFile


This has the effect of removing the NULL byte and anything following it.

Note: NULL-terminated strings are supported in COBOL when specifying a Z"literal".
MOVE Z"123" to field1 would result in the value "123" followed by a X"00" being moved into field1.

View solution in original post

0 Likes
2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Returning char* from C routines in Visual COBOL under Unix

Jump to solution
This is to be expected as a NULL terminated string is a C construct and not a COBOL one.

In COBOL a PIC X data item can contain any character including a X"00" (NULL).

In order to get the expected results in COBOL you should either initialize the field to spaces before calling the C program or extract the data from the field after the call using something like:


    unstring p_szFile delimited by x"00"
       into p_szFile


This has the effect of removing the NULL byte and anything following it.

Note: NULL-terminated strings are supported in COBOL when specifying a Z"literal".
MOVE Z"123" to field1 would result in the value "123" followed by a X"00" being moved into field1.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Returning char* from C routines in Visual COBOL under Unix

Jump to solution
Ok, I got it. Thanks.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.