Home » Developer & Programmer » Precompilers, OCI & OCCI » Data lost after adding Null terminator for VARCHAR data recieved from database (Oracle 11g, Linux)
Data lost after adding Null terminator for VARCHAR data recieved from database [message #486024] Mon, 13 December 2010 00:34 Go to next message
SantoshKS
Messages: 4
Registered: December 2010
Location: Bangalore
Junior Member
Hi,

We are migrating a proc application as described below.
Old Env: UNIX
Old DB: Oracle 8i

New Env: Linux
New DB: Oracle 11g

New modules are successfully compiled in Linux environment. But we are facing issues in writing the output of VARCHAR datatype to a file.

Please find below the extract of code.
EXEC SQL BEGIN DECLARE SECTION;
varchar mcolmnvarchar[4];
EXEC SQL END DECLARE SECTION;

EXEC SQL DECLARE crs CURSOR FOR
SELECT NVL(colmn,' ') FROM table1

memset(mcolmnvarchar.arr,'\0',4); //Was added for only Linux migration. Not present in unix env.

EXEC SQL FETCH c1 INTO :mcolmnvarchar;

cout << "Data at Stage one"<< mcolmnvarchar << endl;
mcolmnvarchar.arr[mcolmnvarchar.len]='\0';
cout << "Data at Stage two"<< mcolmnvarchar << endl;
fprintf(fptr,"%-4s",mcolmnvarchar.arr);

Above code works absolutely fine in Unix env with Oracle 8i.
But with Linux env & Oracle 11g it is not working. No compilation or run time errors. Data at Stage one prints the output of database properly. But after null terminator code, Data at Stage two statement prints without any value. Value is lost after null terminator code.

Please help how to proceed. As this is a production issue, we need urgent solution on this. Kindly help.



[Updated on: Mon, 13 December 2010 00:36]

Report message to a moderator

Re: Data lost after adding Null terminator for VARCHAR data recieved from database [message #486046 is a reply to message #486024] Mon, 13 December 2010 03:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
What is the datatype of COLMN column?

Regards
Michel
Re: Data lost after adding Null terminator for VARCHAR data recieved from database [message #486141 is a reply to message #486046] Mon, 13 December 2010 21:23 Go to previous messageGo to next message
SantoshKS
Messages: 4
Registered: December 2010
Location: Bangalore
Junior Member
Datatype in database is VARCHAR2.

Re: Data lost after adding Null terminator for VARCHAR data recieved from database [message #486149 is a reply to message #486141] Mon, 13 December 2010 23:36 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
VARCHAR2(what)?

Regards
Michel
Re: Data lost after adding Null terminator for VARCHAR data recieved from database [message #486163 is a reply to message #486149] Tue, 14 December 2010 01:39 Go to previous messageGo to next message
SantoshKS
Messages: 4
Registered: December 2010
Location: Bangalore
Junior Member
VARCHAR2(4)
Re: Data lost after adding Null terminator for VARCHAR data recieved from database [message #486173 is a reply to message #486163] Tue, 14 December 2010 02:11 Go to previous messageGo to next message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
So you have to declare your variable to be able to store 5 characters to be able to include the final '\0'.

Quote:
But with Linux env & Oracle 11g it is not working. No compilation or run time errors. Data at Stage one prints the output of database properly. But after null terminator code, Data at Stage two statement prints without any value. Value is lost after null terminator code.

This sentence is not clear; Do you mean there is null ('\0') character inside your string?
Copy and paste the data you have from a direct database query and the output you get from the program in both servers.

Regards
Michel
Re: Data lost after adding Null terminator for VARCHAR data recieved from database [message #486229 is a reply to message #486173] Tue, 14 December 2010 06:15 Go to previous messageGo to next message
SantoshKS
Messages: 4
Registered: December 2010
Location: Bangalore
Junior Member
There is no null character inside the string when retrieved from database. After i append a null char at the end of string data is lost.
But, yes. you were correct. Changing the length from 4 to 5 worked. But the same works fine in Unix & Oracle 8i env with length 4 only. What could be the reason?

[Updated on: Tue, 14 December 2010 06:25]

Report message to a moderator

Re: Data lost after adding Null terminator for VARCHAR data recieved from database [message #486234 is a reply to message #486229] Tue, 14 December 2010 06:27 Go to previous message
Michel Cadot
Messages: 68625
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Different versions, different behaviours. I think 8i one was a (now fixed) bug and "works" by chance.

Regards
Michel
Previous Topic: PRO C
Next Topic: How can I compile
Goto Forum:
  


Current Time: Thu Mar 28 11:47:14 CDT 2024