Problem with sscanf

This is a discussion on Problem with sscanf within the C Programming forums, part of the General Programming Boards category; 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); ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    %c is for 1 character
    to read string upto 4 characters
    %4s

    to read string stopping on , upto 4 characters
    %4[^,]
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 08: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, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21