readfile.cpp

来自「一个长度信息管理系统。主要包含了界面编程和对文件操作。」· C++ 代码 · 共 149 行

CPP
149
字号
#include "stdafx.h"
#include "ReadFile.h"


bool ReadLineFile::OpenFile(CString strFileName)
{
	CString strext;
	CString fileName;

	bool	bsucceed  = false;

	// 未指定文件,选择文件
	if(strFileName.GetLength() == 0)
	{
		static char szFilters[]="兼容文件(*.txt)|*.txt|所有文件 (*.*)|*.*||";
	
	
		CFileDialog dlg(TRUE, "txt", NULL, OFN_HIDEREADONLY, szFilters, NULL);
		if (dlg.DoModal() == IDOK)
		{	
			strext = dlg.GetFileExt();
			strext.MakeLower();			
			fileName = dlg.GetPathName();
			bsucceed = true;
		}	
		else return false;
	}

	// 指定文件,打开
	else
	{
		strext = GetFileExt(strFileName);
		strext.MakeLower();			
		fileName.Format("%s", strFileName);
		bsucceed = true; 
	}

	m_pfile = fopen(fileName, "r");
	if(m_pfile == NULL)
		return false;	

	return bsucceed;
}

void ReadLineFile::CloseFile()
{
	if(m_pfile)
	{
		fclose(m_pfile);
		m_pfile = NULL;
	}
}

//	读操作
void	ReadLineFile::StartRead(int ColSum)
{
	char line[250];
	fseek(m_pfile, 0l, SEEK_SET);
	for(int i =0 ; i < ColSum; i++)
	{
		if(fgets(line, 250, m_pfile) ==  NULL)
			return ;
	}
}

//	
bool	ReadLineFile::ReadLineMid(char*	linechar, long Pos, char cPar, char *lineMid)
{
	long l_PosBegin = -1, l_PosEnd;
	long index = 0;
	long	charNum = 0;
	char  c = linechar[index];
	while (c != 10 ) // '\n'
	{
		if(Pos == 0)
		{
			l_PosBegin= 0;
			if (linechar[index] == cPar)
			{
				l_PosEnd = index;
				strcpy(lineMid, &linechar[l_PosBegin]);
				lineMid[l_PosEnd - l_PosBegin] = 0;
				return true;
			}
			
			index++;
			c = linechar[index];
			if(c == 0)
				break;
			
			continue;
		}

		if (linechar[index] == cPar)
		{
			if(l_PosBegin == -1)
			{
				if(++charNum == Pos)
					l_PosBegin = index;			
			}
			else		
			{
				l_PosEnd = index;
				strcpy(lineMid, &linechar[l_PosBegin+1]);
				lineMid[l_PosEnd - l_PosBegin - 1] = 0;
				return true;
			}				
		}
		index++;
		c = linechar[index];
		if(c == 0)
			break;
					
	}

	if(l_PosBegin >= 0)
	{
		strcpy(lineMid, &linechar[l_PosBegin ? l_PosBegin+ 1 : l_PosBegin]);		
		lineMid[index - (l_PosBegin ? l_PosBegin+ 1 : l_PosBegin)] = 0;
		return true;
	}
	return false;
}

bool ReadLineFile::GetNextChar(char **line)
{
	if (fgets(*line, 200, m_pfile) == NULL)
	{		
		return false;
	}
	return true;
}

//--------------------------
//	函数  GetFileExt
//	功能   得到文件的扩展名
//	输入  
//	输出 
//----------------------------
/*
CString GetFileExt(const CString& path)
{
	ASSERT(path.GetLength());
	int pos = path.ReverseFind('.');
	if (pos >= 0)
		return path.Right(path.GetLength() - pos - 1);
	return "";
} 
*/	

⌨️ 快捷键说明

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