Changing decimal point

This is a discussion on Changing decimal point within the C Programming forums, part of the General Programming Boards category; Hey ppl. I am currently doing some coursework at uni and would like some help on the locale.h. I am ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    4

    Changing decimal point

    Hey ppl. I am currently doing some coursework at uni and would like some help on the locale.h.

    I am reading a line of a csv file into a buffer and then putting each number into a member of a structure. The thing is the numbers are using a comma's as the decimal point instead of a dot. Can someone tell me how i would use the locale.h to change how the decimal point is read.

    thanx

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    maybe this might help?? The simplest solution would be to read the line into a buffer then replace all the commas with a period.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    There is other ways to do it but I have been told to try and use the locale.h.

    anyway i found this code which tells me what the decimal point is :
    Code:
    #include<stdio.h>
    #include<locale.h>
    
    int main()
    {
        struct lconv *ptr;
        char *decimal;
    
        setlocale(LC_ALL, "");
        ptr = localeconv();
        decimal = ptr->decimal_point;
    }
    I fully understand this code but I cant figure out how to change the locale. Can someone please tell me the code to replace the decimal point?

    thanx

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    replacing the comma with a decimal point is pretty trivel. What is the field separator in that file -- probably tabs since commas are used in the numbers?

    Code:
    char *p;
    FILE *fp = fopen("somefile.cvs","r");
    char iobuf[255]; // or some other bigger or smaller number
    while( fgets(iobuf,sizeof(iobuf),fp) )
    {
       while( (p = strchr(iobuf,',')) != 0)
            *p = '.';
       // now break the line into its individual fields assumed to be        floating-pointer 
      // integers separated by tabs
      p = strtok(buffer,"\t");
      while(p)
      {
         float a = atof(p);
         // put a into whatever struct you want it.
         p = strtok(0,"\t");
      }
    }
    Last edited by Ancient Dragon; 03-01-2006 at 03:24 PM.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I found that setting local to German will work. But you will want to change it back when done.
    Code:
    #include<stdio.h>
    #include<locale.h>
    #include <string.h>
    #include <math.h>
    
    void rdfile()
    {
    	float a;
    	FILE *fp = fopen("somefile.cvs","r");
    	if(fp == 0)
    		return;
    	while( fscanf(fp,"%f",&a) > 0)
    	{
    		printf("%f\n",a);
    	}
    	fclose(fp);
    
    }
    
    int main()
    {
        struct lconv *ptr;
        char *decimal;
    
        setlocale(LC_ALL, "German");
        ptr = localeconv();
        decimal = ptr->decimal_point;
    	rdfile();
    	return 0;
    }
    [edit]it appears setlocal() does not affect system-wide local setting -- only the currently running program instance. so there is really no need to set it back to original value unless you need it reset for some other reason. [/edit]
    Last edited by Ancient Dragon; 03-01-2006 at 04:00 PM.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    thanx for the help. I will just change the country to german.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    4
    Can you tell me where I can find a list of locals and the details of each one?

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I have no idea. you might read some of the links thatgoogle produces.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 03:39 PM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  4. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 08:52 AM
  5. observing whether a point is in a triangle???
    By hebele in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2003, 04:38 PM

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