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

📄 csascii.cpp

📁 原理同简单的编译器分析
💻 CPP
字号:
// csascii.cpp: implementation of the csascii class.
//
//////////////////////////////////////////////////////////////////////

#include "csascii.h"
#include <stdlib.h>
#include <fstream>
using namespace std;

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


//把每个field存在data 中
int csascii::readrecord() 
{
	for (int fieldnum = 0; fieldnum < fieldcount; fieldnum++ )
	{
		data[fieldnum] = new field( *datafile );
		if ( data[fieldnum]->eof() ) 
		{
			return 0;//如果文件结束,则返回0
		}
	}
	return 1;
}

//构造函数
//把filename打开
//并计算一行的单词个数
//开辟一个动态空间 指向 存放field对象的数组  的首地址
//调用readrecord()
csascii::csascii( const char * filename ) 
{
	int quote = 0;
	fieldcount = 0;
	{
		ifstream infile( filename );
		if( !infile ) 
		{	
			cerr << "could not open " << filename;
			exit( 1 );
		}
		char c;

		//计算一行单词的个数
		while ( infile.get( c ), c != '\n' )
		{
			if ( c == '\"' )
			{
				quote = !quote;
			}
			if( c == ',' &&  !quote)
			{
				fieldcount++;
			}
		}

	}

	//找回最后一个
	fieldcount++;
    
	//开辟一个动态空间 指向 存放field对象的数组  的首地址
	data = new field *[fieldcount];

	datafile = new ifstream(filename);

	readrecord();
}

//释放相应的动态内存
csascii::~csascii() 
{
	for ( int i = 0; i < fieldcount; i++ )
	{
		delete data[i];
	}

	delete []data;
	delete datafile;
}

//对field释放并清零,以供下一次使用
csascii::next() 
{
	if( data )
	{
		for (int i = 0; i < fieldcount; i++)
		{
			delete data[i];
			data[i] = 0;
		}
	}

	return readrecord();
}

//重载[],能检测是否越界
field& csascii::operator []( int index ) 
{
	if( index >= fieldcount )
	{
		cerr << " index too large for number of field in record";
		exit( 1 );
	}

	return *( data[index] );

}

⌨️ 快捷键说明

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