Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #112376

Re: Splitting strings into arrays with sscanf

From Real Troll <Real.Troll@Trolls.com>
Newsgroups comp.lang.c
Subject Re: Splitting strings into arrays with sscanf
Date 2017-06-16 17:30 -0400
Organization Aioe.org NNTP Server
Message-ID <oi1ie7$658$1@gioia.aioe.org> (permalink)
References <oi14f9$edg$3@dont-email.me>

Show all headers | View raw


On 16/06/2017 18:33, DFS wrote:
> How do you use sscanf to split a line into pieces and add each piece 
> to an array?
>
>
> #include <stdio.h>
> #include <string.h>
> int main (int argc, char *argv[])
> {
>     char line[50]    = "";
>     char lbls[4]     = "";
>     char data[10][4] = "";
>
>     strcpy(line,"Name,Quarter,Product,Sales");
>     sscanf(line, "%s,%s,%s,%s", lbls[0],lbls[1],lbls[2],lbls[3]);
>     printf("%s\n",lbls[2]);
>
>     strcpy(line,"Jim,Q1,computer,100");
>     sscanf(line, "%s,%s,%s,%s", 
> data[0][0],data[0][1],data[0][2],data[0][3]);
>
>     return 0;
> }
>
>
> tcc throws an error that doesn't make sense:
> Line 7: error: ',' expected (got ";")
>
>

Would something like this work for you?


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
     char line[50], lbls1[10], lbls2[10], lbls3[10], lbls4[10] ;

     strcpy(line,"Name Quarter Product Sales");
     sscanf(line, "%s %s %s %s", lbls1, lbls2, lbls3, lbls4);
     printf("%s %s %s %s\n",lbls1, lbls2, lbls3, lbls4);

     return 0;
}


Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Splitting strings into arrays with sscanf DFS <nospam@dfs.com> - 2017-06-16 13:33 -0400
  Re: Splitting strings into arrays with sscanf scott@slp53.sl.home (Scott Lurndal) - 2017-06-16 17:46 +0000
    Re: Splitting strings into arrays with sscanf DFS <nospam@dfs.com> - 2017-06-16 14:02 -0400
      Re: Splitting strings into arrays with sscanf scott@slp53.sl.home (Scott Lurndal) - 2017-06-16 18:15 +0000
        Re: Splitting strings into arrays with sscanf Keith Thompson <kst-u@mib.org> - 2017-06-16 12:28 -0700
    Re: Splitting strings into arrays with sscanf Keith Thompson <kst-u@mib.org> - 2017-06-16 12:20 -0700
  Re: Splitting strings into arrays with sscanf Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2017-06-16 13:47 -0400
    Re: Splitting strings into arrays with sscanf scott@slp53.sl.home (Scott Lurndal) - 2017-06-16 18:01 +0000
      Re: Splitting strings into arrays with sscanf Barry Schwarz <schwarzb@dqel.com> - 2017-06-16 12:03 -0700
        Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 15:33 -0400
          Re: Splitting strings into arrays with sscanf Keith Thompson <kst-u@mib.org> - 2017-06-16 13:16 -0700
      Re: Splitting strings into arrays with sscanf Keith Thompson <kst-u@mib.org> - 2017-06-16 12:31 -0700
      Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 15:45 -0400
  Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 14:07 -0400
  Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 14:22 -0400
    Re: Splitting strings into arrays with sscanf DFS <nospam@dfs.com> - 2017-06-16 15:36 -0400
      Re: Splitting strings into arrays with sscanf "James R. Kuyper" <jameskuyper@verizon.net> - 2017-06-16 17:17 -0400
  Re: Splitting strings into arrays with sscanf Barry Schwarz <schwarzb@dqel.com> - 2017-06-16 12:06 -0700
  Re: Splitting strings into arrays with sscanf Real Troll <Real.Troll@Trolls.com> - 2017-06-16 17:30 -0400
  Re: Splitting strings into arrays with sscanf ike@iceland.freeshell.org - 2017-06-17 16:49 +0000
  Re: Splitting strings into arrays with sscanf Richard Damon <Richard@Damon-Family.org> - 2017-06-17 18:25 -0400

csiph-web