2013-06-03 3 views
1

MFC CFile 쓰기 함수에 대한 질문이 있습니다.
MFC 응용 프로그램에 대해 배우고이 Save As 및 Write 함수에서 멈추었습니다. TestButton을 클릭하면 다른 이름으로 저장 대화 상자가 나타나 txt 파일로 저장하라는 메시지가 나타납니다. 그때 하나의 File에 두 개의 버퍼를 쓰기는 DataFile로 작성하고 새로운 라인은 쓸 때마다 만들기 어떻게CFile 쓰기 함수

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")); 
} 

질문은?
출력 파일이 저장되지만 새 행으로 이동하지 않고 하나의 버퍼 (TEST1) 만 두 번 저장됩니다.

답변

2

사실이 코드는 다음 프로그래밍 문

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

, 옳다면 코드에 문제는 단지 하나의 의미가 buffer0 먼저 파일의 문자 배열을 만들고 그래서 마지막으로 buffer1로 교체한다는 것입니다 가지고 buffer1을 최종 파일 값으로 가져옵니다.

소개 \ n이 같이해야하기 때문에 제대로 작동하지\ 연구 \ 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")); 
+0

감사합니다. 코드가 작동합니다. 그러나'File = newchar .....'에 세 번째 값을 추가하고자한다면'double'이 어떻게됩니까? >> double처럼, buffer0, buffer1 << 또한 txt 파일에서 두 개의 버퍼를 구분하기 위해 쉼표를 어떻게 추가합니까? >> 같은'TEST0, TEST1' << 등등. – Ashton

+1

@Ashton 편집 후 업데이트 된 코드를 확인하십시오. 필요한 것을 얻을 수 있습니다. –

+0

대단히 감사합니다 !! 하지만 이중으로'% lf'을 사용해서는 안됩니까 ?? – Ashton