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

📄 ptf.h

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 H
字号:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>

enum Access  {IN, OUT};	// defines the data flow of the file

class PascalTextFile
{
	private:
      	fstream f;                    // C++ file stream
      	char fname[64];               // file name
      	Access accesstype;            // data flow is IN or OUT
      	int isOpen;                   // used for Reset
      	void Error(char *msg);        // used to print errors

  	public:
      	PascalTextFile(void);         // constructor       
      	void Assign(char *filename);  // sets file name
      	void Reset(void);             // open for input
     	void Rewrite(void);           // open for output
      	int EndFile(void);            // read eof flag
      	void Close(void);             // close file
      	int PRead(char A[], int n);   // read n chars into A
      	void PWrite(char A[],int n);  // write n chars from A
};

// report an error and terminate the program
void PascalTextFile::Error(char *msg)
{
   cerr << msg << endl;
   exit(1);
}

// constructor. mark file as unopened
PascalTextFile::PascalTextFile(void)
{
	isOpen = 0;
}

// Assign initializes the file name
void PascalTextFile::Assign(char *filename)
{
	strcpy(fname, filename);
}

// if file already open, seeks to beginning
// of the file; otherwise, opens file fname
// for input
void PascalTextFile::Reset(void)
{
	if (isOpen)
		if (accesstype == IN)
			// seek to the beginning of the file
   			f.seekg(0,ios::beg);
   		else
   			Error("Cannot reset an output file");
   	else
   	{
   		// access is input
   		accesstype = IN;
   		// open fname for input and if it does
   		// not exist, do not create an emtpy one
      	f.open(fname, ios::in | ios::nocreate);
      	if (f != NULL)
      		isOpen++;
      	else
      		Error("Reset: Cannot open Pascal file");
   }
}

// opens file fname for output
void PascalTextFile::Rewrite(void)
{
	if (isOpen)
   		Error("Pascal file already open");
   	else
   	{
		// access is output
   		accesstype = OUT;
   		// open fname for output. truncate the file
   		// if it exists
      	f.open(fname, ios::out | ios::trunc);
      	if (f != NULL)
      		isOpen++;
      	else
      		Error("Rewrite: Pascal file cannot be opened");
   }
}

void PascalTextFile::Close(void)
{
	if (isOpen)
   		// close the file
   		f.close();
   isOpen = 0;
}

// determine whether end of file has been reached
int PascalTextFile::EndFile(void)
{
	if(!isOpen)
      Error("file is closed");

	// use the stream method eof
	return f.eof();
}

// read n characters into array A. return the
// actual number of characters read
int PascalTextFile::PRead(char A[], int n)
{
	if (accesstype == OUT)
		Error("PRead: file is for output");
	else if (n < 0) 
		Error("PRead: Invalid character count");
   
	if(!isOpen)
		Error("PRead: file is closed");

	// read up to n characters from stream f
	f.read(A, n);
	// return the number of characters read. this
	// may be less than n if end of file was reached
	return f.gcount();
}

// writes n characters from array A to the file     
void PascalTextFile::PWrite(char A[], int n)
{   
	if (accesstype == IN)
		Error("PWrite: invalid file access operation");
	else if (n < 0) 
		Error("PWrite: Invalid character count");

	if(!isOpen)
		Error("PWrite: file is closed");

	// write n bytes to f
	f.write(A,n);
}

⌨️ 快捷键说明

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