Thread: Problem with sscanf

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    12

    Problem with sscanf

    Hi,

    I am having a problem with using sscanf on the following string:
    $one,184943.000,3409.0533,N,11817.0188,W

    this is my sscanf function:
    sscanf(line,"%4c,%lf,%f,N,%f,W",t1,&t2,&t3,&t4);

    What am I doing wrong?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    %c is for 1 character
    to read string upto 4 characters
    %4s

    to read string stopping on , upto 4 characters
    %4[^,]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    Thanks for your reply, I realised that I was not specific about the problem that I am having.
    I can read the start string (one) but I am not able to read 3409.0533 instead of that it gives me the third value 11817.0188 and in the third float, it gives me the third value again.
    I cannot figure out why.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Code:
    #include <stdio.h>
    
    int main()
    {
    	const char* line = "$one,184943.000,3409.0533,N,11817.0188,W";
    	char t1[5] = "";
    	double t2 = 0.0;
    	float t3 = 0.0f, t4 =0.0f;
    	int ret = sscanf(line,"%4[^,],%lf,%f,N,%f,W",t1,&t2,&t3,&t4);
    	return 0;
    }
    Have no problem with this
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    12
    Strange thanks, ill look into it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. problem with sscanf
    By nevrax in forum C Programming
    Replies: 19
    Last Post: 04-14-2007, 10:59 PM
  3. sscanf problem
    By LordShme in forum C Programming
    Replies: 5
    Last Post: 12-05-2006, 09:09 PM
  4. Weird problem with sscanf
    By g4j31a5 in forum C++ Programming
    Replies: 17
    Last Post: 10-04-2006, 09:16 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM