473,385 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to escape string

I am trying to save a big text string into MySQL, but I guess i need
escape the string firstly, anybody knows any escape function in c for
that? or any other suggests ?

Jun 8 '07 #1
3 8625
qilin wrote:
I am trying to save a big text string into MySQL, but I guess i need
escape the string firstly, anybody knows any escape function in c for
that? or any other suggests ?
There isn't a standard C one, but there a MySQL one, search your docs
for escape.

--
Ian Collins.
Jun 8 '07 #2

"qilin" <gd*****@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
>I am trying to save a big text string into MySQL, but I guess i need
escape the string firstly, anybody knows any escape function in c for
that? or any other suggests ?
Here's some code from my comma-separated valuses to C struct converter.
You'll have to change it to make the FILE * into a pointer to a buffer -
remember to make it at least twice as big as the string plus one in case
passed a string full of escapes.

/*
write a string as a C string to stream
Params: str - the string
fp - output file
Notes:handles escapes
*/
int dumpstring(const char *str, FILE *fp)
{
char buff[64];

if(!str)
{
fprintf(fp, "0");
return 0;
}
fputc('\"', fp);
while(*str)
{
if(escaped(*str))
{
escape(buff, *str);
fprintf(fp, "%s", buff);
}
fputc(*str, fp);
str++;
}
fputc('\"', fp);

return 0;
}

/*
is a character escaped?
Params: ch - character to test
Returns: 1 if escaped, 0 if normal
*/
int escaped(int ch)
{
return strchr("\\\a\b\n\r\t\'\"\f\v", ch) ? 1 : 0;
}

/*
get the escape sequence for a character
Params: out - output buffer (currently max 2 + nul but allow for more)
ch - the character to escape
*/
void escape(char *out, int ch)
{
switch(ch)
{
case '\n':
strcpy(out, "\\n"); break;
case '\t':
strcpy(out, "\\t"); break;
case '\v':
strcpy(out, "\\v"); break;
case '\b':
strcpy(out, "\\b"); break;
case '\r':
strcpy(out, "\\r"); break;
case '\f':
strcpy(out, "\\f"); break;
case '\a':
strcpy(out, "\\a"); break;
case '\\':
strcpy(out, "\\\\"); break;
case '\'':
strcpy(out, "\\\'"); break;
case '\"':
strcpy(out, "\\\""); break;
default:
out[0] = (char) ch; break;
out[1] = 0;
}
}

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jun 9 '07 #3
Malcolm McLean wrote:
>
"qilin" <gd*****@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr oups.com...
>I am trying to save a big text string into MySQL, but I guess i need
escape the string firstly, anybody knows any escape function in c for
that? or any other suggests ?

Here's some code from my comma-separated valuses to C struct converter.
You'll have to change it to make the FILE * into a pointer to a buffer -
remember to make it at least twice as big as the string plus one in case
passed a string full of escapes.
Could this be a classic case of the wrong answer to an OT post?

In the context of MySQL, escaping a string involves escaping characters
that can cause problems in a database, that's why I told the OP to check
the MySQL documentation for the well documented appropriate function.

--
Ian Collins.
Jun 9 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
3
by: Ken | last post by:
HI: I'm reading a string that will be displayed in a MessageBox from a resource file. The string in the resource file contains escape sequences so they will be broken up into multiple lines. ...
3
by: Don | last post by:
I am building a string from a combination of hardcoded string literals and user input (via textbox). I know about using @"c:\temp\filename.txt" to ignore escape sequences. Now let's say I have a...
4
by: Guadala Harry | last post by:
I need to place the following into a string... How can I properly escape the % " / < and > characters? <table width="100%" border="0" cellspacing="0" cellpadding="4px" class="hfAll"></Table> ...
12
by: Jeff S | last post by:
In a VB.NET code behind module, I build a string for a link that points to a JavaScript function. The two lines of code below show what is relevant. PopupLink = "javascript:PopUpWindow(" &...
3
by: Guadala Harry | last post by:
I'd like to know the answer to the following question so I can know what to expect with regard to other similar uses of escape characters and strings. While everything works fine - I'd like to know...
16
by: sudhir | last post by:
hi how to check escape key is pressed when accepting the string as input. Because I do not want to receive a string if user presses the ESCAPE key.. I used ascii code for comparision but I...
5
by: vlsidesign | last post by:
The printf function returns "warning: unknown escape sequence: \040" for a backslash-space combination. If the ascii decimal number for space is 32 and the backslash is 92, why this particular...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.