Help required with scanset

S

sarfaraz

I have a line e.g.
,123,245,A,BC,,,,,,

I want to get the first 5 comma separated values:

while(scanf("%[^,],%[^,],%[^,],%[^,],%[^,],%*[^\n]\n",a,b,c,d,e) == 5)
{
}

But scanf is exiting on empty string as the first string. Essentially,
what I want is:
a=""
b="123"
c="245"
and so on

It might be the case that in some lines 'b' , 'c' or any other
variable takes an empty string.

Can anybody please help?
I know how to do it with getchar(), But the code is much compact with
scanset.
 
E

Eric Sosman

sarfaraz said:
I have a line e.g.
,123,245,A,BC,,,,,,

I want to get the first 5 comma separated values:

while(scanf("%[^,],%[^,],%[^,],%[^,],%[^,],%*[^\n]\n",a,b,c,d,e) == 5)
{
}

But scanf is exiting on empty string as the first string. Essentially,
what I want is:
a=""
b="123"
c="245"
and so on

It might be the case that in some lines 'b' , 'c' or any other
variable takes an empty string.

Can anybody please help?
I know how to do it with getchar(), But the code is much compact with
scanset.

You're out of luck, I think, because the "%[" directive
matches a *nonempty* sequence of characters. That's the way
the Standard defines it (section 7.19.6.2 paragraph 12).

If I were writing the code, I'd probably read the whole
line into a char[] array and then pick it apart with strchr()
and so on. (Not with strtok(), which treats a consecutive
run of delimiters the same as a single delimiter.) Or if
I were worried about very long lines I might read the input
character-by-character and look for the commas as I went.
 
C

CBFalconer

sarfaraz said:
I have a line e.g.
,123,245,A,BC,,,,,,

I want to get the first 5 comma separated values:

while(scanf("%[^,],%[^,],%[^,],%[^,],%[^,],%*[^\n]\n",a,b,c,d,e) == 5)
{
}

But scanf is exiting on empty string as the first string. Essentially,
what I want is:
a=""
b="123"
c="245"
and so on

Don't use scanf. Try the following. If you predefine the value
'TESTING' you will get a test file.


/* ------- 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 ----------*/

/* ------- 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 ----------*/
 

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

Similar Threads

Help with passing test 3
Need help with this script 4
Help in this program. 2
Help with Loop 0
Help with code 0
Help with my responsive home page 2
Can't solve problems! please Help 0
Help with code plsss 0

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top