0

I'm studying "Headfirst C" and saw some code using [^\n]. Below is part of code, of which name is 'code.c'.

#include <stdio.h>

int main()
{
     float latitude;
     float longitude;
     char info[80];

     scanf("%f, %f, %79[^\n]", &latitude, &longitude, info) ;
     printf("{latitude: %f, longitude: %f, info: %s}", latitude, longitude, info);
}

And What i want to do with this code is to read some data from data.cvs file and store it at 'latitude', 'longitude', 'info' and print it.
Below is gpsdata.cvs file

42.3634, -71.098465, Speed = 21
42.363327,-71.097588,Speed = 23

Result is supposed to be:

data=[
{latitude: 43.363400, longitudeL -71.098465, info: 'Speed = 21'}

I think this is related with [^\n] thing is scanf. What is it? I've never seen this before. Is it kinda new grammar of C11? Please help me!

MD XF
  • 7,860
  • 7
  • 40
  • 71
Chois
  • 75
  • 1
  • 2
  • 7
  • 1
    man-page: http://man7.org/linux/man-pages/man3/scanf.3.html – Deduplicator Jan 31 '15 at 13:00
  • @AntP It's not regular expression. – Yu Hao Jan 31 '15 at 13:00
  • If you want to read from a file where are you opening a file? `[^\n]` just means read till newline char is encountered – Gopi Jan 31 '15 at 13:02
  • @simonzack a subtle difference between the duplicate and this one is this code is about `scanf()` and that answer is about `sscanf()`. When using `"[^..."`, with `scanf()`, scanning continues even if a rare `'\0'` is encountered resulting in _interesting_ effects. With `sscanf()`, a `'\0'` is never encountered. Because of this difference, IMO, a different answer is warranted. But likely some other better matching duplicate exist. – chux - Reinstate Monica Jan 31 '15 at 17:56

0 Answers0