JonMny
asked on
Can someone provide me a sample to read a csv file into an array / simple code to write to a file
MFC is new for me any help would be appreciated, I need some code to write a file with comma separated values and code to read the values into an array
Could yo ube a bit more specific about what you want to store in that file and how the values that are read from it should be organized?
ASKER
Sorry, I didn't give enough info, I need to write 3 string values to a file. Either a comma delimited file that I can read into an array. Or just three lines of text that I can read line by line.
The catch is this is a Unicode project so I can't use Cfile.
Example
ClientName,OrderNumber,Ord erStatus
ClientName
OrderNumber
OrderStatus
Either is fine this is just a sample I am creating
The catch is this is a Unicode project so I can't use Cfile.
Example
ClientName,OrderNumber,Ord
ClientName
OrderNumber
OrderStatus
Either is fine this is just a sample I am creating
>>Example
>>ClientName,OrderNumber,O rderStatus
>>ClientName
>>OrderNumber
>>OrderStatus
implemetation by c
>>ClientName,OrderNumber,O
>>ClientName
>>OrderNumber
>>OrderStatus
implemetation by c
TCHAR srcStr[3][32];
_tcscpy(srcStr[0],_T("ClientName"));
_tcscpy(srcStr[1],_T("OrderNumber"));
_tcscpy(srcStr[2],_T("OrderStatus"));
FILE* fp;
TCHAR* pFileName = _T("test.txt");
fp = _tfopen(pFileName, _T("w+b"));
int i;
if( fp != NULL )
{
const BYTE head[] = {0xff, 0xfe};//BOM(byte-order mark)
fwrite(head, sizeof(BYTE), sizeof(head)/sizeof(BYTE), fp);
for(i=0;i<3;i++)
{
fwrite(srcStr[i], sizeof(TCHAR), _tcslen(srcStr[i]), fp);
fwrite(_T(","),sizeof(TCHAR),_tcslen(_T(",")),fp);
}
}
fclose(fp);
MFC implemetation.
CString srcStr[3];
srcStr[0] = _T("ClientName");
srcStr[1] = _T("OrderNumber");
srcStr[2] = _T("OrderStatus");
CFile file;
LPCTSTR pFileName = _T("test.txt");
if(!file.Open(pFileName,CFile::modeCreate | CFile::modeWrite))
{
//ERROR
}
const BYTE head[] = {0xff, 0xfe};//BOM(byte-order mark)
file.Write(head,2);
int i;
for( i=0;i<3;i++)
{
file.Write(srcStr[i],sizeof(TCHAR)*srcStr[0].GetLength());
file.Write(",",sizeof(TCHAR));
}
file.Close();
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the help!
itsmeandnobodyelse,thank u for your advice.
https://www.experts-exchange.com/questions/20651518/Read-Write-CSV-Files-in-C.html
Best Regards,
DeepuAbrahamK