Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,


I need to export floating numbers in CSV file with decimal symbol as ",(comma)" in english local settings. I did put that numbers in double quotes in order to prevent my numbers from being formatted before exporting to CSV for example "1,23456789" with the delimiter tab . But when I viewed the file it shows "123,456,789" . Seems like my format has been changed even though i have put in double quotes.

Please give me some suggestion.

I am using cstring to format the float to string. Both in excel and notepad, in both the places it is showing wrong value. Below is my code,
C++
CFileStream _fileLog; 
CString strDelimiter = _T("\t"); 
CString strQuote = _T("\""); 
CString strContent; 
strContent = strQuote + _T("1,23456789") + strQuote + strDelimiter + 
             strQuote + _T("9,12345678") + strQuote;
fileLog.Write(strContent);
Posted
Updated 19-Oct-11 23:01pm
v2
Comments
Maximilien 19-Oct-11 7:36am    
Show us the code you use to write to the file (ONLY the few lines that format and write the float values).
Are you using STL (stream) or sprintf to format the float to the string ?
How are you viewing the result ? in notepad or in excel (for example) ?
bunathangaraj 19-Oct-11 7:48am    
I am using cstring to format the float to string. Both in excel and notepad, in both the places it is showing wrong value.
Below is my code,
CFileStream _fileLog;
CString strDelimiter = _T("\t");
CString strQuote = _T("\"");
CString strContent;
strContent = strQuote + _T("1,23456789") + strQuote + strDelimiter + strQuote + _T("9,12345678") + strQuote;
_fileLog.Write(strContent);

str.Replace(_T("."), _T(","));
Chuck O'Toole 19-Oct-11 21:43pm    
Impossible! Everything in this code is a string and all you are writing is one big string. There is no way that just reading it with notepad is going to apply a "comma every 3 digits" type interpretation. You are looking in the wrong place for the file this creates.
bunathangaraj 19-Oct-11 7:49am    
pls ignore the last line in the above code
Richard MacCutchan 19-Oct-11 9:25am    
What are you using to view the file? If you are using Excel and the system decimal separator is "." then Excel will not recognise your values as floating point, but as integers.

Your code is missing a statement to open the file! You are not writing anything that way. Whatever file you look at, it will never change.
 
Share this answer
 
Comments
Chuck O'Toole 20-Oct-11 9:10am    
Nice catch, I didn't see that when I told him he was looking in the wrong place for the file. There is no file (nor spoon) +5
Don't know that this will help...
Guess you'll have to look deeper into CFile and/or CString

Code
C++
#include <iostream>
#include <fstream>
using namespace std;

int main () {

  fstream filestr;
  string delim = "\t", quotes = "\"", content;

  filestr.open ("test.txt", fstream::in | fstream::out | fstream::app);

  content = quotes + "1,23456789" + quotes + delim;
  content += quotes + "9,12345678" + quotes + delim;

  filestr << content;

  filestr.close();

  return 0;
}


Output
"1,23456789"    "9,12345678"    
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900