Convert comma to point

Hi everybody
I would like to know if someone knows a easy way, for example using fstream to verify if a file contains comma "," and if it contains replace it with a point "."
In particular this file will contain numbers with comma so a need to convert it onto point.
Ex: 2,5 --> 2.5
Thank you for your help.
Last edited on
sed "s/,/./g" input-file > output-file

Or do you have to write a program to do it?
Thanks for your help, but I have to write a program to do it, or maybe if it's possible in C++, I need read a file that contains numbers with comma and load these numbers as double variable.
Try with locales
Hi Bazzy,
thanks for your help, probably localeS, is a good way to do that, but can you give me a small example to how use it, please? I'm beginner in C++.
For example I have a file that contains only the number 2,5 how can I get it using that:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <fstream>
#include <locale>
using namespace std;

int main(){

double number;
fstream f;
f.open("myfile.txt",ios::in);

f>>number; //how I use localeS to entry a double that uses comma?
cout<<number;

f.close();
return 0;
}


Thank you very much.
Last edited on
Another way is to read each line from the input file one at a time into a string, then replace all occurrences of ',' with '.'

Use cin.getline() to read a line then use string::replace().
Built in languages locale names change depending on the computer, this will use the default locale for the computer in which is used:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <locale>
#include <fstream>
using namespace std;

int main()
{
     locale myloc(""); //Computer's Default Locale
     double n;
     ifstream f("myfile.txt");
     f.imbue(myloc);
     
     while (!f.eof())
     {
          f >> n;
          cout << n << '\n';
     }
     return 0;
}

To know the name of your locale use myloc.name()

If you want to build your own locale, do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <locale>
#include <fstream>
using namespace std;
class WithComma: public numpunct<char> // class for decimal numbers with comma
{
	protected: char do_decimal_point() const { return ','; } // override the function that gives the decimal separator
};
int main()
{
     locale myloc(  locale(),    // C++ default locale
                  new WithComma);// Own numeric facet
     double n;
     ifstream f("myfile.txt");
     f.imbue(myloc);
     
     while (!f.eof())
     {
          f >> n;
          cout << n << '\n';
     }
     return 0;
}
Last edited on
Hi jsmith and Bazzy,
Thank you jsmith for your help, but in fact I preferred the Bazzy's idea, simply because it doesn't change the file, and I think that it's more fast if I have a big file with i.e. 10000 numbers.

Thank you very much Bazzy, it's a great code, I only found a small problem: if for example I have a file that contains many numbers that 50% uses comma and the rest point, how can I read it and stock in a double variable without problems, I'm asking that because I tried your code putting a number with point, and the program broke.
So is there the possibility to create a if() that analyze all elements of the file and: if it is a correct double (that uses point: i.e. 5.6) it uses the default fstream to get it, or if it contains a comma it uses your code.
Somenthing like that, but I don't know how to create this if():
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.....
locale myloc(  locale(),    // C++ default locale
         new WithComma);// Own numeric facet
double n;
while(!f.eof()){
  if (f.peek() == //double: number with point)
  {
    f>>n;
  }
  else{
    f.imbue(myloc);
    f>>n;
  }
  cout<<n<<endl;
}
Neither solution changes the file. More than likely the relative speeds of the two approaches will be relatively indistinguishable regardless of file size, if only because the amount of time needed to read the file will dominate your execution time.

Having said that, Bazzy's approach is a good generic internationalized approach worthy to be professional-quality software. On the other hand, if this were a school project or something for my own personal use (ie, where internationalization is not a concern), I'd go with the search/replace since it is less code.




In order to have different inputs (with , and .) but always good output, I would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
locale myloc(locale(), new WithComma);
string get; //string used to check if it has comma or dot 
stringstream *temp; //used for temporary stream holding one number per time
double n;
while (!f.eof())
{
     getline(f,get,' ');
     temp = new stringstream(get);
     if ((int)get.find(',')>=0)  temp->imbue(myloc);
     temp->operator >> (n);
     delete temp;
     cout << n << '\n';
}



Ps: I agree with jsmith: if you want to have something simple, just replace , with .
Hi Bazzy,
Great, great, perfect!!!!!!!
It's absolutly that I want, and it's very professional!!!
Thank you very much, for all your help.
Topic archived. No new replies allowed.