473,566 Members | 3,309 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Spaces in C

Hi,

I have a little piece of program here

Basically what it does is, it copies the strings of variable widths. The
basis is until it finds a comma ",". The input is a CSV/Comma Separated
file.

Now the problem is that it is not counting Spaces. For example to read the
following line with the below Program is OK:

123, Hello, 3422C
3994,Hii,39948D

Result: Fine, works;

But if I have the strings like the below;
123, Hi How are you,99399 C

Result: Fails to read after "Hi" because of the space, can you suggest any
code changes to below stub?

Thanks
J
--------------------------
int j = 0;
char c;
c = ptr[0];
ibic[0] = c;
while(c != ',')
{
++j;
c = ptr[j];
ibic[j] = c;
}
ibic[j] = '\0';
return 0;
-------------------------
Feb 13 '06 #1
12 2046

"Joriveek" <no****@please. com> wrote in message
news:ds******** **@reader01.new s.esat.net...
Hi,

I have a little piece of program here

Basically what it does is, it copies the strings of variable widths. The
basis is until it finds a comma ",". The input is a CSV/Comma Separated
file.

Now the problem is that it is not counting Spaces. For example to read the
following line with the below Program is OK:

123, Hello, 3422C
3994,Hii,39948D

Result: Fine, works;

But if I have the strings like the below;
123, Hi How are you,99399 C

Result: Fails to read after "Hi" because of the space, can you suggest any
code changes to below stub?

Thanks
J
--------------------------
int j = 0;
char c;
c = ptr[0];
ibic[0] = c;
while(c != ',')
{
++j;
c = ptr[j];
ibic[j] = c;
}
ibic[j] = '\0';
return 0;
-------------------------


Are you sure the problem is in that stub and not in the routine that reads
and fills 'ptr'?

Rod Pemberton
Feb 13 '06 #2


Joriveek wrote On 02/13/06 11:00,:
Hi,

I have a little piece of program here


Please post the entire thing -- reduce it to its
essentials if it's long, but post a complete compilable
program. When you are sick, do you take your entire
body to the doctor or just send a lock of your hair?

--
Er*********@sun .com

Feb 13 '06 #3
sorry, it is for reading a CSV file;
if there are spaces, it is not working, just reading if it is a continuous
string.
"Rod Pemberton" <do*********@so rry.bitbucket.c mm> wrote in message
news:43******** @news.bea.com.. .

"Joriveek" <no****@please. com> wrote in message
news:ds******** **@reader01.new s.esat.net...
Hi,

I have a little piece of program here

Basically what it does is, it copies the strings of variable widths. The
basis is until it finds a comma ",". The input is a CSV/Comma Separated
file.

Now the problem is that it is not counting Spaces. For example to read
the
following line with the below Program is OK:

123, Hello, 3422C
3994,Hii,39948D

Result: Fine, works;

But if I have the strings like the below;
123, Hi How are you,99399 C

Result: Fails to read after "Hi" because of the space, can you suggest
any
code changes to below stub?

Thanks
J
--------------------------
int j = 0;
char c;
c = ptr[0];
ibic[0] = c;
while(c != ',')
{
++j;
c = ptr[j];
ibic[j] = c;
}
ibic[j] = '\0';
return 0;
-------------------------


Are you sure the problem is in that stub and not in the routine that reads
and fills 'ptr'?

Rod Pemberton

Feb 13 '06 #4

Eric Sosman wrote:

[snip]
When you are sick, do you take your entire
body to the doctor or just send a lock of your hair?


this is really a good one :-))

Feb 13 '06 #5
"Joriveek" <no****@please. com> wrote in message
news:ds******** **@reader01.new s.esat.net...
Hi,

I have a little piece of program here

Basically what it does is, it copies the strings of variable widths. The
basis is until it finds a comma ",". The input is a CSV/Comma Separated
file.

Now the problem is that it is not counting Spaces. For example to read the
following line with the below Program is OK:

123, Hello, 3422C
3994,Hii,39948D

Result: Fine, works;

But if I have the strings like the below;
123, Hi How are you,99399 C

Result: Fails to read after "Hi" because of the space, can you suggest any
code changes to below stub?

Thanks
J
--------------------------
int j = 0;
char c;
c = ptr[0];
ibic[0] = c;
while(c != ',')
{
++j;
c = ptr[j];
ibic[j] = c;
}
ibic[j] = '\0';
return 0;
-------------------------


Try displaying the contents of ptr[], maybe there are no spaces in there
either.
Feb 13 '06 #6
On Mon, 13 Feb 2006 16:00:34 -0000, in comp.lang.c , "Joriveek"
<no****@please. com> wrote:
Hi,

I have a little piece of program here
you didn't post enough of your code. The sample you show doesn't make
any sense.
Basically what it does is, it copies the strings of variable widths. The
basis is until it finds a comma ",". The input is a CSV/Comma Separated
file.


You could try using strchr or strtok

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Feb 18 '06 #7
Mark McIntyre wrote:
"Joriveek" <no****@please. com> wrote:

I have a little piece of program here


you didn't post enough of your code. The sample you show doesn't
make any sense.
Basically what it does is, it copies the strings of variable
widths. The basis is until it finds a comma ",". The input is a
CSV/Comma Separated file.


You could try using strchr or strtok


Here is a routine I just wrote down, totally untested, and not even
compiled yet. After this bunch gets through criticizing it it
should be bullet proof. Until then beware slippery slopes.

#include <stddef.h>

/* copy over the next token from an input string, after
skipping leading blanks (or other whitespace???). The
token is terminated by the first appearance of tokchar,
or by the end of the source string.
The caller must supply sufficient space in token to
receive any token, Otherwise tokens will be truncated.

Returns: a pointer past the terminating tokchar.

This will happily return an infinity of empty tokens if
called with src pointing to the end of a string. Tokens
will never include a copy of tokchar.
*/
const char *toksplit(const char *src, /* Source of tokens */
char tokchar, /* token delimiting char */
char *token, /* receiver of parsed token */
size_t lgh) /* length token can receive */
/* not including final '\0' */
{
while (' ' == *src) *src++;

while (*src && (tokchar != *src)) {
if (lgh) {
*token++ = *src;
--lgh;
}
src++;
}
if (*src && (tokchar == *src)) src++;
*token = '\0';
return src;
} /* toksplit */

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Feb 19 '06 #8
CBFalconer schrieb:
Mark McIntyre wrote:
"Joriveek" <no****@please. com> wrote:
I have a little piece of program here
you didn't post enough of your code. The sample you show doesn't
make any sense.

Basically what it does is, it copies the strings of variable
widths. The basis is until it finds a comma ",". The input is a
CSV/Comma Separated file.


You could try using strchr or strtok

Here is a routine I just wrote down, totally untested, and not even
compiled yet. After this bunch gets through criticizing it it
should be bullet proof. Until then beware slippery slopes.


I did not test it either...

#include <stddef.h>

/* copy over the next token from an input string, after
skipping leading blanks (or other whitespace???). The
token is terminated by the first appearance of tokchar,
or by the end of the source string.
The caller must supply sufficient space in token to
receive any token, Otherwise tokens will be truncated.

Returns: a pointer past the terminating tokchar.

This will happily return an infinity of empty tokens if
called with src pointing to the end of a string. Tokens
will never include a copy of tokchar.
*/
const char *toksplit(const char *src, /* Source of tokens */
char tokchar, /* token delimiting char */
char *token, /* receiver of parsed token */
size_t lgh) /* length token can receive */
/* not including final '\0' */
{
while (' ' == *src) *src++;
ITYM
while (*src && ' ' == *src) src++;
while (*src && (tokchar != *src)) {
if (lgh) {
*token++ = *src;
--lgh;
}
I'd break in an else. Why go through 100000 characters if
five suffice? This may imply a change of the loop structure.
src++;
}
if (*src && (tokchar == *src)) src++;
*token = '\0';
return src;
} /* toksplit */


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Feb 19 '06 #9
Michael Mair wrote:
CBFalconer schrieb:
Mark McIntyre wrote:
"Joriveek" <no****@please. com> wrote:

I have a little piece of program here

you didn't post enough of your code. The sample you show doesn't
make any sense.

Basically what it does is, it copies the strings of variable
widths. The basis is until it finds a comma ",". The input is a
CSV/Comma Separated file.

You could try using strchr or strtok
Here is a routine I just wrote down, totally untested, and not even
compiled yet. After this bunch gets through criticizing it it
should be bullet proof. Until then beware slippery slopes.


I did not test it either...
#include <stddef.h>

/* copy over the next token from an input string, after
skipping leading blanks (or other whitespace???). The
token is terminated by the first appearance of tokchar,
or by the end of the source string.
The caller must supply sufficient space in token to
receive any token, Otherwise tokens will be truncated.

Returns: a pointer past the terminating tokchar.

This will happily return an infinity of empty tokens if
called with src pointing to the end of a string. Tokens
will never include a copy of tokchar.
*/

const char *toksplit(const char *src, /* Source of tokens */
char tokchar, /* token delimiting char */
char *token, /* receiver of parsed token */
size_t lgh) /* length token can receive */
/* not including final '\0' */
{
while (' ' == *src) *src++;


ITYM
while (*src && ' ' == *src) src++;


if *src == ' ' then *src is true, unless ' ' == 0, which conflicts
with the idea that strings are terminated with '\0'.
while (*src && (tokchar != *src)) {
if (lgh) {
*token++ = *src;
--lgh;
}
I'd break in an else. Why go through 100000 characters if
five suffice? This may imply a change of the loop structure.


My attitude is that if a token is over-long and needs to be
truncated, do it and get the pointers set up for the next token.
That way a sequence of calls can always find, say, the third
token. I envision something like:

const char source = "Suitable stuff, , make, tokens";
char token[5];
const char *src = source;
int i;
....
for (i = 0; i < 3; i++) {
src = toksplit(src, ',', token, sizeof(token) - 1);
process(token);
}

finding the third token, "make". I don't think a source string of
length 10000 is especially likely to occur, so I am prepared for
inefficiencies in dealing with it. This would allow tokens to be
abbreviated to their first four chars.
src++;
}
if (*src && (tokchar == *src)) src++;
*token = '\0';
return src;
} /* toksplit */


--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>

Feb 19 '06 #10

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

Similar topics

2
11086
by: Michael Bulatovich | last post by:
I have a simple db to keep track of work/time/projects etc. It has two fields (column) named "start time" and "end time" WITH THE SPACES. I'm trying to do some automation to a form associated with the db and notice that in all the examples I've seen nobody EVER includes spaces in the names of fields. Now I'm getting a sinking feeling.... ...
11
14984
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white spaces(if it more than once between two words). Is there any function we have in C which will find out the tabs and white spaces and returns the...
9
6267
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how can i make a fast Right Trim Function in c,using Binary search kind of fast algorithm ? Offcourse...I can use the classical approach too. like :...
17
11813
by: tommy | last post by:
Hi all, I' m adding strings to some fields in my table via Access. The strings sometimes have trailing spaces and I really need to have it that way, but Access truncates trailing spaces. How can I force Access not to truncate?! Thanx, /Toommy
135
7397
by: Xah Lee | last post by:
Tabs versus Spaces in Source Code Xah Lee, 2006-05-13 In coding a computer program, there's often the choices of tabs or spaces for code indentation. There is a large amount of confusion about which is better. It has become what's known as “religious war” — a heated fight over trivia. In this essay, i like to explain what is the...
3
12403
by: Luke - eat.lemons | last post by:
Hi, Could someone tell me the correct quotation to get around spaces in paths for: Server.MapPath("\\server\share\spaces in name.mdb") I know its bad practice to use spaces but unfortunately i cannot help this.
8
17496
by: Joe Cool | last post by:
I need to map several columns of data from one database to another where the data contains multiple spaces (once occurance of a variable number or spaces) that I need to replace with a single space. What would be the most efficient way to do this? I am using SQL2K. I was thinking a function since I know of no single Transact-SQL command that...
3
4794
by: bstjean | last post by:
Hi everyone, I am trying to find an efficient way to perform a special query. Let me explain what I want. Let's say we are looking for all description that match "this is the target". In fact, I want to find records that match those 4 words in this sequence disregarding the number of spaces (I mean spaces, tabs, Cr, Lf, etc) between...
5
2947
by: brian.j.parker | last post by:
Hey all, I've noticed an obscure little quirk: it appears that if you use a login with trailing spaces on the name, SYSTEM_USER automatically trims those trailing spaces in SQL Server 2000, but not SQL Server 2005. Anybody know if this change in behavior is documented? If it is intentional? Is there a "quick fix" to revert to the old...
8
4727
by: drjay1627 | last post by:
hello, This is my 1st post here! *welcome drjay* Thanks! I look answering questions and getting answers to other! Now that we got that out of the way. I'm trying to read in a string and add the unique words in the string to a map. Eg:
0
7673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7584
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6263
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1202
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.