⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readfile.cpp

📁 在数控机床的坐标文件
💻 CPP
字号:
// ReadFile.cpp: implementation of the CReadFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "NC.h"
#include "ReadFile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CReadFile::CReadFile(CString str)
{
	CString temp;
	CStdioFile file,file_new;
	total=0;		//带子的总数清0
	if(!file.Open(_T(str),CFile::typeText))
	{
		MessageBox("can not open the file!");
		return;
	}
	while(file.ReadString(temp))
	{
		int lineNumber=0;   //统计每个带子中数据的行数
		temp.TrimLeft(_T(" "));
		temp.TrimRight(_T(" "));
		if(0<temp.GetLength()&&temp.GetLength()<3)
		{
			CString  file1=temp;
			file.ReadString(temp);
			temp.TrimLeft(_T(" "));
			temp.TrimRight(_T(" "));
			if(temp.CompareNoCase("start"))
			{
				MessageBox("此文件不是合法文件,请核实!!","文件类型",MB_OK);
				return;
			}
			CString filename;
			filename=file1;
			filename+=".txt";
			if(!file_new.Open(_T(filename),CFile::modeCreate | CFile::modeWrite|CFile::typeText))
			{
				MessageBox("创建临时文件失败!","临时文件创建",MB_OK);
				return;
			}
			CString strLine;
			while(file.ReadString(strLine))
			{   
				CString strLine0;
			    strLine0=strLine;
				strLine.TrimLeft(_T(" "));
				strLine.TrimRight(_T(" "));
				if(strLine.CompareNoCase("end"))
				{
					strLine0+="\r\n";
					file_new.WriteString(strLine0);
					lineNumber++;
					
				}
				else
				{	total++;		//带子的总数加1
				    maxLine[total]=lineNumber;	//保存每个带子里面的数据行数,防止
												//ReadLine成员函数出现越界操作
					file_new.Close();
					break;
				}
			}
		}
	}
	file.Close();
}

CReadFile::~CReadFile()
{
	CString str;
	char buf[20];
	for(int i=1;i<=total;i++)
	{
	memset(buf,0,20);
	itoa(i,buf,10);
	str=buf;
	str+=_T(".txt");
	char filename[20];
	strcpy(filename,(const char *)str);
	DeleteFile(filename);
	}
}

void CReadFile::ReadLine(int order, int line)
{
	CString str;
//	CStdioFile file;
	if(order<1||order>total)
	{
		CString str1;
		str1.Format("越界!一共只有%d个带子",total);
		MessageBox(str1);
		return;
	}
	if(line>maxLine[order]||line<1)
	{
		CString str2;
		str2.Format("在%d个带子中,最多%d行",order,maxLine[order]);
		MessageBox(str2);
		return;
	}
	char buf[20];
	itoa(order,buf,10);
	str=buf;
	str+=_T(".txt");
	char filename[20];
	strcpy(filename,(const char *)str);
    FILE *fp;
	if((fp=fopen(filename,"r"))==NULL)
	{
		MessageBox("读入指定行失败!",MB_OK);
		return;
	}
	if(line==1)
	{
		fscanf(fp,"%d ",&degree);
	//	CString tempstr;
	//	tempstr.Format("degree is %d",degree);
	//	MessageBox(tempstr);
		rewind(fp);
	}
	else
	{   
		fscanf(fp,"%d",&degree);
		for(int i=2;i<=line;i++)
		{
			fscanf(fp,"%lf %lf %lf %lf %lf %lf %lf",
			&x,&y,&z,&vx,&vy,&vz,&sl);
		}
	//	CString tempstr;
	//	tempstr.Format("x,y,z,vx,vy,vz,gx,gy,gz,sl is %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
	//		x,y,z,vx,vy,vz,gx,gy,gz,sl);
	//	MessageBox(tempstr);
		rewind(fp);		
	}
	fclose(fp);
}

⌨️ 快捷键说明

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