1

I have got a question about the MFC CFile write function.
I am learning about MFC application and stuck at this Save As and write function. When I click the TestButton, a save as dialog box would pop out prompting to save as txt file.

void CLearnDlg::OnBnClickedButtonTest()
{
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;


for (int i=0; i<2; i++)
{
    File = (("%S, %S\n\n"), buffer0, buffer1);
    GetLength = strlen(File);
    DataFile.Write(File, GetLength);
}
DataFile.Close();
MessageBox(_T("OK"));
}

Question is how do I write two buffer together into a single File then write it into the DataFile and making a new line every time it write?
The output file is saved but only one buffer (TEST1) is saved twice without going to a new line.

Ashton
  • 83
  • 1
  • 3
  • 12

1 Answers1

2

Actually there is something wrong with your code if your code is right , then your programming statement

File = (("%S, %S\n\n"), buffer0, buffer1);

Has only one meaning is that , create File character array first with buffer0 and replace it with buffer1 so finally you will get buffer1 as final File value.

About \n not working properly because it should be like, \r\n

So your final program may look like,

      // 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();
}

[EDIT]

    // 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"));
Santosh Dhanawade
  • 1,756
  • 14
  • 29
  • thank you, the code works. But if I want to add a third value into the `File = newchar .....` which is a `double` how do I do it? Like >> double, buffer0, buffer1 << Also how do I add a comma to separate the two buffer in the txt file? Like >> `TEST0, TEST1` << and so on. – Ashton Jun 03 '13 at 05:07
  • 1
    @Ashton please see updated code after edit you will get what you required. – Santosh Dhanawade Jun 03 '13 at 05:19
  • THANK YOU VERY MUCH!! But for double shouldn't I use `%lf`?? – Ashton Jun 03 '13 at 05:40
  • @Ashton depends on size and number of character you want. Means if you want big floating number then you should go through %lf else %f is much enough. – Santosh Dhanawade Jun 03 '13 at 05:43
  • Okay! Thank you very much! Had completed this project earlier due to your help! Once again thank you! – Ashton Jun 03 '13 at 05:51