myfile.cpp

来自「客户端程序」· C++ 代码 · 共 75 行

CPP
75
字号
// MyFile.cpp: 实现CMyFile类.
//
#include "stdafx.h"
#include "MyFile.h"


CMyFile myfile;

//---------------------------------------------------------------------------
CMyFile::CMyFile()
{
}

//---------------------------------------------------------------------------
CMyFile::~CMyFile()
{
}

//---------------------------------------------------------------------------
BOOL CMyFile::FileExists(LPCTSTR lpszFileName)
{
	DWORD dwAttributes = GetFileAttributes(lpszFileName);
    if (dwAttributes == 0xFFFFFFFF)
        return FALSE;

	if ((dwAttributes & FILE_ATTRIBUTE_DIRECTORY) 
		 ==	FILE_ATTRIBUTE_DIRECTORY)
	{
			return FALSE;
	}
	else{
		return TRUE;
	}
}

//---------------------------------------------------------------------------
FILE* CMyFile::GetFilePointer(LPCTSTR lpszFileName)
{
    FILE *fp;
	if(FileExists(lpszFileName)){
		// 打开已有文件进行写数据.
		fp=fopen(lpszFileName,"r+b");
	}
	else{
		// 创建新文件进行写数据.
		fp=fopen(lpszFileName,"w+b");
        if(fp==NULL) AfxMessageBox("error");
	}
	
   	return fp;
}

//---------------------------------------------------------------------------
DWORD CMyFile::GetFileSizeByName(LPCTSTR lpszFileName)
{
	if(!FileExists(lpszFileName)) return 0;
	struct _stat ST; 
	// 获取文件长度.
	_stat(lpszFileName, &ST);
	UINT nFilesize=ST.st_size;
	return nFilesize; 
}

//---------------------------------------------------------------------------
// 从全程文件名中提取短文件名.
CString CMyFile::GetShortFileName(LPCSTR lpszFullPathName)
{
   CString strFileName=lpszFullPathName;
   CString strShortName;
   strFileName.TrimLeft();
   strFileName.TrimRight();
   int n=strFileName.ReverseFind('/');
   strShortName=strFileName.Right(strFileName.GetLength()-n-1);
   return strShortName;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?