Contents

?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
MFC 파일 입출력 공부하기 좋은 예제
파일 입출력 샘플 (한줄씩 읽어서 다른 파일에 쓰기)

----- files.h -----
#include <afxwin.h>

class CFilesApp: public CWinApp
{
    public:
    BOOL InitInstance();
};

class CMainWindow : public CFrameWnd
{
    public:
    CMainWindow();
    afx_msg void OnPaint();
    afx_msg void OnLButtonDown(UINT nFlags,CPoint point);
    afx_msg void OnRButtonDown(UINT nFlags,CPoint point);
    protected:
    DECLARE_MESSAGE_MAP();
};

----- files.cpp -----
#include "Files.h"

CFilesApp theApp;

BOOL CFilesApp::InitInstance()
{
    theApp.m_pMainWnd = new CMainWindow;
    theApp.m_pMainWnd->ShowWindow(SW_SHOW);
    theApp.m_pMainWnd->UpdateWindow();
    return TRUE;
}

BEGIN_MESSAGE_MAP(CMainWindow,CFrameWnd)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()

ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()

CMainWindow::CMainWindow()
{
    Create(NULL,"HELLO");
}

void CMainWindow::OnPaint()
{
    //
}

void CMainWindow::OnLButtonDown(UINT nFlags,CPoint point)
{
    CClientDC dc(this); // 사용자영역 DC  얻어옴
    UINT Y_pos=0; // 위치 조정 변수
    CString Strlist;  // 파일 내용 저장 변수
    CStdioFile file; // CStdioFile에 힘을 file로 
    
    if(file.Open(_T("File.txt"),CStdioFile::modeRead))
    //CStdio~ 힘을 가진 file이 파일 열기 모드는 읽기
    {
        for(int i=0;i<file.GetLength();i++) // file에 능력중에 줄 길이 읽기
        {
            file.ReadString(Strlist); // 한줄씩 읽기
            dc.TextOut(0,Y_pos+=20,Strlist); // 출력하기
        }
    }
    file.Close() // 닫기
}

void CMainWindow::OnRButtonDown(UINT nFlags,CPoint point)
{
    CString test; // 저장할 값 넣는 변수
    CStdioFile file; // 위와 동
    
    if(file.Open("WriteFile.txt",CFile::modeCreate| CFile::modeWrite)) // 파일 생성 또는 쓰기
    {
        test = "낙엽 
사랑 
무사"; // 들어갈 내용
        file.WriteString(test); // 파일 쓰기
    }
    file.Close(); // 닫기
}


?

List of Articles
번호 분류 제목 글쓴이 날짜 조회 수
843 Develop '2014 모바일 개발 트렌드' 발표자료입니다. file hooni 2014.10.02 2532
842 Develop Aspect Oriented Programming in Objective-C hooni 2015.05.18 3249
841 Develop DDay Memo 1.9.4 소스코드 secret hooni 2015.10.03 0
840 Develop GCM 사용하기 2 (단말에 GCM 구현하기) file hooni 2013.07.06 24652
839 Develop GCM 사용하기 3 (JSP로 GCM 푸시 서버 만들기) 4 file hooni 2013.07.06 27129
838 Develop git 브런치 배우기 (링크) hooni 2013.07.09 22078
837 Develop GPL, AGPL, MPL,.. 한눈에 보는 오픈소스SW 라이선스 file hooni 2014.10.14 2059
836 Develop How to Test SMTP AUTH using Telnet hooni 2018.04.05 6156
835 Develop JSON, BSON 변환 file hooni 2013.04.23 13129
834 Develop Laravel 5 Failed opening required bootstrap/../vendor/autoload.php hooni 2018.01.24 6109
833 Develop Mac OS 에 Jenkins 설치하기 (Homebrew) 2 file hooni 2017.03.15 13246
832 Develop macOS에 node, npm 설치하기 (homebrew) file hooni 2021.11.06 4544
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 71 Next
/ 71