tags:

views:

70

answers:

3

I want to generate a csv file containing numbers and I want Excel to read that file automatically.

However, Excel uses a different csv format depending on the locale settings. For example:

English:

  • Text separator: ,
  • Decimal separator: .

Spanish:

  • Text separator: ;
  • Decimal separator: ,

Is it possible to automatically detect those settings in C++ so I can generate the file according to them?

I know I can obtain the decimal separator using a locale object, but I do not know how to obtain the text separator.

This question is related to: CSV is actually … Semicolon Separated Values . However, I do not want to read the file but to generate it and I want to automatically detect the settings.

A: 

You can, but not with 100% certainty (but very close to it). Detecting the text separator can be done by analyzing the file (or a decent sample) and determining the frequencies of each character in each record. The text separator should be the same (or nearly the same) for every record. It's frequency will be one less than the number of fields in each record.

A good analyzer will not count characters that are protected by double quotes, as these are embedded and not separators (ie, don't count the comma in "Boston, MA 02066", as it is protected by the double quotes).

Decimal separators can be determined after you've determined the text separator. Just look for adjacent numbers and see what's separating them (that's not the previously determined text separator).

Marc Bernier
Oops, now I see that you want to generate such a file, not read it.
Marc Bernier
A: 

Use CultureInfo.TextInfo.ListSeparator.

Tergiver
Oops, I just noticed you asked specifically for C++. Eric gave the correct answer.
Tergiver
+2  A: 

I don't think that standard C++ has a notion of "text separator", and that this is Windows-specific. You could thus use GetLocaleInfo with the LOCALE_SLIST flag.

Éric Malenfant
Thank you all. This worked:GetLocaleInfo(LOCALE_NAME_USER_DEFAULT, LOCALE_SLIST, data, sizeof(data)/sizeof(WCHAR));I assume there is no "standard C++" equivalent.
J. Calleja