469,890 Members | 1,649 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,890 developers. It's quick & easy.

Problem: Displaying TEXT datatype with C program

Hi,

I'm currently trying to display the contents of a field which datatype
is set to TEXT, I'm trying to output this through a C program.

the line is as follows:

while((row = mysql_fetch_row(result)) != NULL)
{
printf("SID: %d\t\tSensor: %s",(row[1] ? row[1] : "NULL"));
}
I have trued %c and %s in to print the Sensor name, however %s
crashes the program and %c produces a smiley face.

Can anyone pls advise?

Thanks
Thomas
Jul 20 '05 #1
1 1833
Thomas wrote:
Hi,

I'm currently trying to display the contents of a field which datatype
is set to TEXT, I'm trying to output this through a C program.

the line is as follows:

while((row = mysql_fetch_row(result)) != NULL)
{
printf("SID: %d\t\tSensor: %s",(row[1] ? row[1] : "NULL"));
}
I have trued %c and %s in to print the Sensor name, however %s
crashes the program and %c produces a smiley face.


The docs say that you can't rely on the string being null-terminated for
BLOB and CLOB data. Printf's %s control by itself will keep on pulling
bytes from the string parameter until it finds a null terminator, or
crashes by overflowing something. So you need to tell it to stop.

Read about mysql_field_lengths():
http://dev.mysql.com/doc/mysql/en/my...h_lengths.html

You might be able to use field precision in the printf %s conversion.
For example (I haven't tested this, but it might get you on the right
track):

while((row = mysql_fetch_row(result)) != NULL)
{
unsigned long *lengths = mysql_field_lengths();
printf("SID: %d\t\tSensor: %.*s",
(int) lengths[1],
(row[1] ? row[1] : "NULL"));
}

Regards,
Bill K.
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Tom Pearson | last post: by
1 post views Thread by Rob Parker | last post: by
1 post views Thread by Rob Parker | last post: by
3 posts views Thread by Newbie | last post: by
reply views Thread by Chris Ericoli | last post: by
4 posts views Thread by Vigneshwar Pilli via DotNetMonster.com | last post: by
5 posts views Thread by dav3 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.