LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-29-2020, 10:34 PM   #1
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Rep: Reputation: Disabled
comma separated values


I have a data stream, e.g.
Code:
1, 3, 5.89, 7, 15.4
i.e. integer, integer, float, integer, float, separated by commas.
I have tried
Code:
int i0, i1, i2;
float f0, f1;
char c;
fscanf(fp, "%d [^,] %d [^,] %f [^,] %d [^,] %f", &i0, &i1, &f0, &i2, &f1);
// and also
fscanf(fp, "%d %c %d %c %f %c %d %c %f", &i0, &c, &i1, &c, &f0, &c, &i2, &c, &f1);
and every variation that I could think of without success.
There's gotta be a simple answer.
 
Old 01-30-2020, 12:54 AM   #2
rigor
Member
 
Registered: Sep 2003
Location: 19th moon ................. ................Planet Covid ................Another Galaxy;............. ................Not Yours
Posts: 705

Rep: Reputation: Disabled
Hi piobair!


I'm expecting that you are working with the C language. I created a file scan_em.c containing some changes to your code:
Code:
# include <stdio.h>


/*  scan_em.c  */

int main(  int argc ,  char *argv[]  )
{
    int     i0 ,  i1 ,  i2 ;
    float   f0 ,  f1 ;
    char    c[100] ;
    int     result ;


    result = fscanf(  stdin , "%d, %d, %f, %d, %f" ,  &i0 ,  &i1 ,  &f0 ,  &i2 , &f1 ) ;
    printf(  "First result=%d\n" ,  result  ) ;

    result = fscanf(  stdin , "%d%[, ]%d%[, ]%f%[, ]%d%[, ]%f",  &i0 ,  c ,  &i1 ,  c ,  &f0 ,  c ,  &i2 ,  c ,  &f1  ) ;
    printf(  "Second result=%d\n" ,  result  ) ;
    
    printf(  "f1=%f\n" ,  f1  ) ;
    
    printf(  "c=`%s`\n" ,  c  ) ;
}
Created a file data.txt:
Code:
1, 3, 5.89, 7, 15.4
1, 3, 5.89, 7, 15.4
compiled scan_em.c ran it with data.txt as stdin, and got this output:
Code:
First result=5
Second result=9
f1=15.400000
c=`, `
Hope this helps.
 
Old 01-30-2020, 07:24 AM   #3
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Original Poster
Rep: Reputation: Disabled
Ah! the %[, ] does the trick.
I was leaving out the %.
thank you.
 
Old 01-30-2020, 08:40 AM   #4
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Original Poster
Rep: Reputation: Disabled
I replied before I tested.
// scantest.c
Code:
// gcc scantest.c -lm

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

void main(){
  char bufr[] = {"1, 3, 5.89, 7, 15.4"};
  int i0, i1, i2;
  float f0, f1;

  printf("bufr = %s\n", bufr);
  sscanf(bufr, "%d%[,]%d%[,]%f%[,]%d%[,]%f", &i0, &i1, &f0, &i2, &f1);
  printf("i0=%d, i1=%d, f0=%f, i2=%d, f1=%f\n", i0, i1, f0, i2, f1);
}
results in a segmentation error
 
Old 01-30-2020, 09:06 AM   #5
piobair
Member
 
Registered: Aug 2013
Distribution: Debian, Ubuntu
Posts: 267

Original Poster
Rep: Reputation: Disabled
Fixed. The %[,] needs to be saved to a &c
 
Old 01-30-2020, 09:25 AM   #6
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
You could use strtok.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] splitting of comma separated values file (horizontal) into a list (vertical ) list Donoughue Linux - Newbie 3 10-20-2011 01:18 PM
How to delete Comma in a comma separated file with double quotes as quote character pklcnu Linux - Newbie 2 03-24-2009 05:50 PM
help with comma separated values and what should be a simple script. zaber Programming 10 03-06-2008 12:58 PM
bash syntax: looping through a comma-separated list David the H. Linux - General 10 09-06-2007 10:23 AM
Reading comma-separated data from file MeLassen Programming 4 04-04-2004 02:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration