Problem getting a string with blanks in it

D

ddk1965

Hello,

I am reading from a comma separated file cvs some date.
The data look like this:

"John Poe",3.4,100
"Alice",7,250

I use this routine to find the 3 fields:

void parsecsv (char *pfld[], long *n, char *pstr) {
/* extracts fields from a string containing a csv record */
*n=1;
pfld[0]=pstr;
while(*pstr) {
if(*pstr == ',') {
*pstr=0;
pfld[*n]=pstr+1;
++(*n);
}
++pstr;
}
}


in my main.c:

static char dlnbuf[MAXSTR], *pfld[20];
static long nfld;

parsecsv(pfld,&nfld,dlnbuf); /* read the fields into separated
parts into pfld */

for(k=0; pfld[0][k]; k++) if(pfld[0][k]=='"') pfld[0][k]=' '; /
* to remove the " from the name */
sscanf(pfld[0], "%s", &name);

Now my problem:

I get only the first part of the name till it reaches a blank

so I get

John
Alice

and not

John Poe
Alice

What I am doing wrong?

Tx,

Danny
 
N

Nate Eldredge

ddk1965 said:
Hello,

I am reading from a comma separated file cvs some date.
The data look like this:

"John Poe",3.4,100
"Alice",7,250 [...]
sscanf(pfld[0], "%s", &name); [...]
I get only the first part of the name till it reaches a blank

Read carefully in your manual about the behavior of the "%s" format
specifier for scanf/sscanf.

Anyway, what is that sscanf supposed to accomplish?
 
D

ddk1965

ddk1965 said:
I am reading from a comma separated file cvs some date.
The data look like this:
"John Poe",3.4,100
"Alice",7,250 [...]
        sscanf(pfld[0], "%s", &name); [...]
I get only the first part of the name till it reaches a blank

Read carefully in your manual about the behavior of the "%s" format
specifier for scanf/sscanf.

Anyway, what is that sscanf supposed to accomplish?

Thanks I solved my problem with printf instead.

rgds,

Danny
 
D

ddk1965

ddk1965 said:
Hello,
I am reading from a comma separated file cvs some date.
The data look like this:
"John Poe",3.4,100
"Alice",7,250
[...]
sscanf(pfld[0], "%s", &name);
[...]
I get only the first part of the name till it reaches a blank
Read carefully in your manual about the behavior of the "%s" format
specifier for scanf/sscanf.
Anyway, what is that sscanf supposed to accomplish?
Thanks I solved my problem with printf instead.

How?

sprintf(name,"%s",pfld[0]);
 
J

Jens Thoms Toerring

ddk1965 said:
ddk1965 said:
I am reading from a comma separated file cvs some date.
The data look like this:
"John Poe",3.4,100
"Alice",7,250
[...]
sscanf(pfld[0], "%s", &name);
[...]
I get only the first part of the name till it reaches a blank
Read carefully in your manual about the behavior of the "%s" format
specifier for scanf/sscanf.
Anyway, what is that sscanf supposed to accomplish?
Thanks I solved my problem with printf instead.
How?

sprintf(name,"%s",pfld[0]);

Wouldn't a simple (and more light-weight)

strcpy( name, pfld[ 0 ] );

be enough?
Regards, Jens
 
D

ddk1965

ddk1965 said:
ddk1965 wrote:
Hello,
I am reading from a comma separated file cvs some date.
The data look like this:
"John Poe",3.4,100
"Alice",7,250
[...]
sscanf(pfld[0], "%s", &name);
[...]
I get only the first part of the name till it reaches a blank
Read carefully in your manual about the behavior of the "%s" format
specifier for scanf/sscanf.
Anyway, what is that sscanf supposed to accomplish?
Thanks I solved my problem with printf instead.
How?
sprintf(name,"%s",pfld[0]);

Wouldn't a simple (and more light-weight)

strcpy( name, pfld[ 0 ] );

be enough?
                    Regards, Jens

Yep great idea.

Tx
 
C

CBFalconer

ddk1965 said:
I am reading from a comma separated file cvs some date.
The data look like this:

"John Poe",3.4,100
"Alice",7,250

I use this routine to find the 3 fields:

I didn't go over your code. However, try this routine (most of it
is the self testing code, enabled by defining TESTING):

/* ------- file tknsplit.c ----------*/
#include "tknsplit.h"

/* copy over the next tkn from an input string, after
skipping leading blanks (or other whitespace?). The
tkn is terminated by the first appearance of tknchar,
or by the end of the source string.

The caller must supply sufficient space in tkn to
receive any tkn, Otherwise tkns will be truncated.

Returns: a pointer past the terminating tknchar.

This will happily return an infinity of empty tkns if
called with src pointing to the end of a string. Tokens
will never include a copy of tknchar.

A better name would be "strtkn", except that is reserved
for the system namespace. Change to that at your risk.

released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
Revised 2006-06-13 2007-05-26 (name)
*/

const char *tknsplit(const char *src, /* Source of tkns */
char tknchar, /* tkn delimiting char */
char *tkn, /* receiver of parsed tkn */
size_t lgh) /* length tkn can receive */
/* not including final '\0' */
{
if (src) {
while (' ' == *src) src++;

while (*src && (tknchar != *src)) {
if (lgh) {
*tkn++ = *src;
--lgh;
}
src++;
}
if (*src && (tknchar == *src)) src++;
}
*tkn = '\0';
return src;
} /* tknsplit */

#ifdef TESTING
#include <stdio.h>

#define ABRsize 6 /* length of acceptable tkn abbreviations */

/* ---------------- */

static void showtkn(int i, char *tok)
{
putchar(i + '1'); putchar(':');
puts(tok);
} /* showtkn */

/* ---------------- */

int main(void)
{
char teststring[] = "This is a test, ,, abbrev, more";

const char *t, *s = teststring;
int i;
char tkn[ABRsize + 1];

puts(teststring);
t = s;
for (i = 0; i < 4; i++) {
t = tknsplit(t, ',', tkn, ABRsize);
showtkn(i, tkn);
}

puts("\nHow to detect 'no more tkns' while truncating");
t = s; i = 0;
while (*t) {
t = tknsplit(t, ',', tkn, 3);
showtkn(i, tkn);
i++;
}

puts("\nUsing blanks as tkn delimiters");
t = s; i = 0;
while (*t) {
t = tknsplit(t, ' ', tkn, ABRsize);
showtkn(i, tkn);
i++;
}
return 0;
} /* main */

#endif
/* ------- end file tknsplit.c ----------*/

/* ------- file tknsplit.h ----------*/
#ifndef H_tknsplit_h
# define H_tknsplit_h

# ifdef __cplusplus
extern "C" {
# endif

#include <stddef.h>

/* copy over the next tkn from an input string, after
skipping leading blanks (or other whitespace?). The
tkn is terminated by the first appearance of tknchar,
or by the end of the source string.

The caller must supply sufficient space in tkn to
receive any tkn, Otherwise tkns will be truncated.

Returns: a pointer past the terminating tknchar.

This will happily return an infinity of empty tkns if
called with src pointing to the end of a string. Tokens
will never include a copy of tknchar.

released to Public Domain, by C.B. Falconer.
Published 2006-02-20. Attribution appreciated.
revised 2007-05-26 (name)
*/

const char *tknsplit(const char *src, /* Source of tkns */
char tknchar, /* tkn delimiting char */
char *tkn, /* receiver of parsed tkn */
size_t lgh); /* length tkn can receive */
/* not including final '\0' */

# ifdef __cplusplus
}
# endif
#endif
/* ------- end file tknsplit.h ----------*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top