473,218 Members | 1,530 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,218 software developers and data experts.

insert a char in a char array

i have a bit of a problem.
im parsing a record string using strtok but im encountering back to
back whitespaces (\t\t) due to empty fields from my database export.
the STRTOK function reads up to the LAST \t in a consecutive
arrangement. this of course skews my fields offsetting them.
annoying.... yes.
however i may have 2 solutions:

1----if i can replace all instances of "\t\t" with "\t \t" or anything
other than space. id be happy.

OR

2----replace all instances of "\t\t" with, say, "*", add "*" to the
list of delimiters of STRTOK and for each token extraction, somehow
find a way to FIND OUT which one it used. this way i could then use IF
statements on each extraction based on a variable updated from the
PREVIOUS extraction.
in other words; if the previous extraction delimited with "*" then
dont even DO this one. and then itll move on to the next token after
maybe manually storing a null to the current field variable.

but how do i DO this?
how can i check which delimiter was used.
and if not, back to the 1st solution, how do i INSERT a space thereby
moving the following characters to the right which will of course mean
that the array has to be +1 larger. HOW?
Jul 19 '05 #1
6 15057
"Sean Bartholomew" <ad****@rivaband.com> wrote...
i have a bit of a problem.
im parsing a record string using strtok but im encountering back to
back whitespaces (\t\t) due to empty fields from my database export.
the STRTOK function reads up to the LAST \t in a consecutive
arrangement. this of course skews my fields offsetting them.
annoying.... yes.
however i may have 2 solutions:

1----if i can replace all instances of "\t\t" with "\t \t" or anything
other than space. id be happy.

OR

2----replace all instances of "\t\t" with, say, "*", add "*" to the
list of delimiters of STRTOK and for each token extraction, somehow
find a way to FIND OUT which one it used. this way i could then use IF
statements on each extraction based on a variable updated from the
PREVIOUS extraction.
in other words; if the previous extraction delimited with "*" then
dont even DO this one. and then itll move on to the next token after
maybe manually storing a null to the current field variable.


OR

3----quit using strtok and simply look for the next \t and extract
what is between them in your string.

Victor
Jul 19 '05 #2
Might be simpler and more efficient to just re-make strtok() to _not_ skip
over multible delimiters. The other options you mentioned would be: a)
unreliable (if you replace all the '\t\t' with '*', what happens if there's
an '*' in there before?), and b) would require alot of fun processing
(expanding '\t \t' would require extra space in a buffer for that ' ', not
to mention an extra buffer).

Just find your implimentation of LIBC's strtok.c file (if you have a
compiler that didn't come with your LIBC source (most of the high-grade
compilers do: gcc, MSVC++, and CodeWarrior, to name a few), you can likely
Google for it, or ask here to see if anyone has a compatible source), and
create a new strtok2() function in your own projects source (unless you want
to create & distribute your own LIBC variant), copying it mostly. Somewhere
in it, you should see a while-loop that does nothing but check to see if a
certain character is a delimiter, and if so, goes to the next character.
All you have to do is make this while-loop into an if-statement, and you're
good to go. Noteing that the change is such a minor one (changing a single
'while' into an 'if'), I'm curious as to why LIBC doesn't allready have a
standardized function for this one -- anybody know?.

Hope this helps
- Alex

Here's the code based off my local MSVC++7 LIBC implimentation (sans
original comments & multi-threaded support) to illustrate the entire change,
which isn't recomended for use with other compilers (which can and will have
issues with this):

--------------------------

char * __cdecl strtok (
char * string,
const char * control
)
{
unsigned char *str;
const unsigned char *ctrl = control;

unsigned char map[32];
int count;

static char *nextoken;

for (count = 0; count < 32; count++)
map[count] = 0;

do {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
} while (*ctrl++);

if (string)
str = string;
else
str = nextoken;

/* To make strtok2() _not_ skip over multible delimiters, just
change this while() into an if()! */
// while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
if ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
str++;

string = str;

for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) {
*str++ = '\0';
break;
}

nextoken = str;

if ( string == str )
return NULL;
else
return string;
}

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003

Jul 19 '05 #3
"Alex Lyman" <alex lyman @ earthlink.net> wrote in message news:<GL*****************@newsread1.news.pas.earth link.net>...
Might be simpler and more efficient to just re-make strtok() to _not_ skip
over multible delimiters. The other options you mentioned would be: a)
unreliable (if you replace all the '\t\t' with '*', what happens if there's
an '*' in there before?), and b) would require alot of fun processing
(expanding '\t \t' would require extra space in a buffer for that ' ', not
to mention an extra buffer).

Just find your implimentation of LIBC's strtok.c file (if you have a
compiler that didn't come with your LIBC source (most of the high-grade
compilers do: gcc, MSVC++, and CodeWarrior, to name a few), you can likely
Google for it, or ask here to see if anyone has a compatible source), and
create a new strtok2() function in your own projects source (unless you want
to create & distribute your own LIBC variant), copying it mostly. Somewhere
in it, you should see a while-loop that does nothing but check to see if a
certain character is a delimiter, and if so, goes to the next character.
All you have to do is make this while-loop into an if-statement, and you're
good to go. Noteing that the change is such a minor one (changing a single
'while' into an 'if'), I'm curious as to why LIBC doesn't allready have a
standardized function for this one -- anybody know?.

Hope this helps
- Alex

Here's the code based off my local MSVC++7 LIBC implimentation (sans
original comments & multi-threaded support) to illustrate the entire change,
which isn't recomended for use with other compilers (which can and will have
issues with this):

--------------------------

char * __cdecl strtok (
char * string,
const char * control
)
{
unsigned char *str;
const unsigned char *ctrl = control;

unsigned char map[32];
int count;

static char *nextoken;

for (count = 0; count < 32; count++)
map[count] = 0;

do {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
} while (*ctrl++);

if (string)
str = string;
else
str = nextoken;

/* To make strtok2() _not_ skip over multible delimiters, just
change this while() into an if()! */
// while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
if ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
str++;

string = str;

for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) {
*str++ = '\0';
break;
}

nextoken = str;

if ( string == str )
return NULL;
else
return string;
}

i tried dropping it in but i get a wad of errors starting with a
missing ";" on the 1st line.
Jul 19 '05 #4
"Alex Lyman" <alex lyman @ earthlink.net> wrote in message news:<GL*****************@newsread1.news.pas.earth link.net>...
Might be simpler and more efficient to just re-make strtok() to _not_ skip
over multible delimiters. The other options you mentioned would be: a)
unreliable (if you replace all the '\t\t' with '*', what happens if there's
an '*' in there before?), and b) would require alot of fun processing
(expanding '\t \t' would require extra space in a buffer for that ' ', not
to mention an extra buffer).

Just find your implimentation of LIBC's strtok.c file (if you have a
compiler that didn't come with your LIBC source (most of the high-grade
compilers do: gcc, MSVC++, and CodeWarrior, to name a few), you can likely
Google for it, or ask here to see if anyone has a compatible source), and
create a new strtok2() function in your own projects source (unless you want
to create & distribute your own LIBC variant), copying it mostly. Somewhere
in it, you should see a while-loop that does nothing but check to see if a
certain character is a delimiter, and if so, goes to the next character.
All you have to do is make this while-loop into an if-statement, and you're
good to go. Noteing that the change is such a minor one (changing a single
'while' into an 'if'), I'm curious as to why LIBC doesn't allready have a
standardized function for this one -- anybody know?.

Hope this helps
- Alex

Here's the code based off my local MSVC++7 LIBC implimentation (sans
original comments & multi-threaded support) to illustrate the entire change,
which isn't recomended for use with other compilers (which can and will have
issues with this):

--------------------------

char * __cdecl strtok (
char * string,
const char * control
)
{
unsigned char *str;
const unsigned char *ctrl = control;

unsigned char map[32];
int count;

static char *nextoken;

for (count = 0; count < 32; count++)
map[count] = 0;

do {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
} while (*ctrl++);

if (string)
str = string;
else
str = nextoken;

/* To make strtok2() _not_ skip over multible delimiters, just
change this while() into an if()! */
// while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
if ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
str++;

string = str;

for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) {
*str++ = '\0';
break;
}

nextoken = str;

if ( string == str )
return NULL;
else
return string;
}

i tried dropping it in but i get a wad of errors starting with a
missing ";" on the 1st line.
Jul 19 '05 #5

"Sean Bartholomew" <ad****@rivaband.com> wrote in message
news:b8**************************@posting.google.c om...
i tried dropping it in but i get a wad of errors starting with a
missing ";" on the 1st line.


This was not unexpected. You'll note my statement: "isn't recomended for
use with other compilers (which can and will have issues with this)"
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003
Jul 19 '05 #6
DOH!

Sometime in the future, maybe I'll learn to actually look at a new algoritim
implimentation before submitting it.
The desired behavior is not one that my below recommendation and code
follow. Instead, the aforementioned 'while' ('if') should be removed
entirely. The code as-was will skip at most 2 delimiters (being an idiot, I
forgot that that while-statement actually starts at the character after the
last \0-d token delimiter). Removing it would get rid of this entirely,
letting the for-loop just below that while-statement take care of where the
delimiters are.

My bad.

- Alex
"Alex Lyman" <alex lyman @ earthlink.net> wrote in message
news:GL*****************@newsread1.news.pas.earthl ink.net...
Might be simpler and more efficient to just re-make strtok() to _not_ skip
over multible delimiters. The other options you mentioned would be: a)
unreliable (if you replace all the '\t\t' with '*', what happens if there's an '*' in there before?), and b) would require alot of fun processing
(expanding '\t \t' would require extra space in a buffer for that ' ', not
to mention an extra buffer).

Just find your implimentation of LIBC's strtok.c file (if you have a
compiler that didn't come with your LIBC source (most of the high-grade
compilers do: gcc, MSVC++, and CodeWarrior, to name a few), you can likely
Google for it, or ask here to see if anyone has a compatible source), and
create a new strtok2() function in your own projects source (unless you want to create & distribute your own LIBC variant), copying it mostly. Somewhere in it, you should see a while-loop that does nothing but check to see if a
certain character is a delimiter, and if so, goes to the next character.
All you have to do is make this while-loop into an if-statement, and you're good to go. Noteing that the change is such a minor one (changing a single 'while' into an 'if'), I'm curious as to why LIBC doesn't allready have a
standardized function for this one -- anybody know?.

Hope this helps
- Alex

Here's the code based off my local MSVC++7 LIBC implimentation (sans
original comments & multi-threaded support) to illustrate the entire change, which isn't recomended for use with other compilers (which can and will have issues with this):

--------------------------

char * __cdecl strtok (
char * string,
const char * control
)
{
unsigned char *str;
const unsigned char *ctrl = control;

unsigned char map[32];
int count;

static char *nextoken;

for (count = 0; count < 32; count++)
map[count] = 0;

do {
map[*ctrl >> 3] |= (1 << (*ctrl & 7));
} while (*ctrl++);

if (string)
str = string;
else
str = nextoken;

/* To make strtok2() _not_ skip over multible delimiters, just
change this while() into an if()! */
// while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
if ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
str++;

string = str;

for ( ; *str ; str++ )
if ( map[*str >> 3] & (1 << (*str & 7)) ) {
*str++ = '\0';
break;
}

nextoken = str;

if ( string == str )
return NULL;
else
return string;
}

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.536 / Virus Database: 331 - Release Date: 11/3/2003
Jul 19 '05 #7

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

Similar topics

15
by: Jack | last post by:
I have a text file of data in a file (add2db.txt) where the entries are already entered on separate lines in the following form: INSERT INTO `reviews` VALUES("", "Tony's", "Lunch", "Great...
2
by: Niraj | last post by:
Hi, I am trying to do bulk insert of binary data (array of bytes) in an Oracle table. The data type of the table is BLOB. I am using Oracle Objects for OLE (OO4O) in C++. The binary data that I...
1
by: Chris Fink | last post by:
I am receiving xml documents from a customer without a reference to a doctype. I know what the Doctype DTD should be need to insert the declaration as follows <?xml version="1.0"...
7
by: tano | last post by:
Hello, I have to insert a char in the middle of a string, I have written two functions but I don't know what is the better? The problem is: if I use malloc() I copy all the string with the new...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
7
by: Kevin Lawrence | last post by:
Hi all I want to do "INSERT INTO Table (Blob) Values('blobdataasstring')". ...rather than using the parameter driven method, is it possible? And if so what encoder do I use to convert the...
9
by: anachronic_individual | last post by:
Hi all, Is there a standard library function to insert an array of characters at a particular point in a text stream without overwriting the existing content, such that the following data in...
6
by: reppisch | last post by:
Hi Ng, I have a multiset for keeping elements sorted in a container but key values may also be equal. Is there any guaranteed traversal order within the duplicate keys of a multimap? When...
6
by: TheRealDan | last post by:
Hi all. I'm having a problem with a special characters. I have php script that reads from an xml file and writes to a mysql db. It's a script called phptunest that I found on the net, although the...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.