其实,有什么问题你的代码,如果你的代码是正确的,那么你的编程语句
File = (("%S, %S\n\n"), buffer0, buffer1);
只有一个意思是,先创建文件的字符数组与buffer0与缓冲器1替换所以最后你会得到buffer1作为最终的文件值。
关于\ n不能正常工作,因为它应该是这样的,\ r \ n
所以你的最终方案可能是什么样子,
// TODO: Add your control notification handler code here
CString m_strPathName;
char* File;
TCHAR szFilters[] =
_T ("Text files (*.txt)¦*.txt¦All files (*.*)¦*.*¦¦");
CFileDialog dlg (FALSE, _T ("txt"), _T ("*.txt"),
OFN_OVERWRITEPROMPT, szFilters);
if (dlg.DoModal() == IDOK)
m_strPathName = dlg.GetPathName();
CFile DataFile(m_strPathName, CFile::modeReadWrite | CFile::modeCreate);
char buffer0[100] = "TEST0";
char buffer1[100] = "TEST1";
int GetLength;
File = new char[strlen(buffer0)+strlen(buffer1)+2];
for (int i=0; i<2; i++)
{
strcpy(File,buffer0);
strcat(File,buffer1);
strcat(File,"\r\n");
GetLength = strlen(File);
DataFile.Write(File, GetLength);
}
DataFile.Close();
MessageBox(_T("OK"));
CDialogEx::OnOK();
}
[编辑]
// TODO: Add your control notification handler code here
CString m_strPathName;
char* File;
TCHAR szFilters[] =
_T ("Text files (*.txt)¦*.txt¦All files (*.*)¦*.*¦¦");
CFileDialog dlg (FALSE, _T ("txt"), _T ("*.txt"),
OFN_OVERWRITEPROMPT, szFilters);
if (dlg.DoModal() == IDOK)
m_strPathName = dlg.GetPathName();
CFile DataFile(m_strPathName, CFile::modeReadWrite | CFile::modeCreate);
char buffer0[100] = "TEST0";
char buffer1[100] = "TEST1";
int GetLength;
File = new char[strlen(buffer0)+strlen(buffer1)+2];
for (int i=0; i<2; i++)
{
double doublevalue;
doublevalue = 1035.25414;
sprintf(File,"%s,%s,%f\r\n", buffer0, buffer1,doublevalue); //Dumping data string and double data saparated with comma
GetLength = strlen(File);
DataFile.Write(File, GetLength);
sprintf(File,"%f>>>%s>>>%s\r\n", doublevalue,buffer1,buffer0); //Dumping data double and string data saparated with >>
GetLength = strlen(File);
DataFile.Write(File, GetLength);
}
DataFile.Close();
MessageBox(_T("OK"));
谢谢,代码有效。但是,如果我想添加第三个值到'File = newchar .....'这是一个'double',我该怎么做呢?像>> double,buffer0,buffer1 <<还有,如何添加一个逗号来分隔txt文件中的两个缓冲区?像>> TEST0,TEST1' <<等等。 – Ashton
@Ashton编辑后请看到更新后的代码,你会得到你需要的。 –
非常感谢!但是,对于双重我不应该使用'%lf'? – Ashton