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

📄 myedit.cpp

📁 这是一些c++例程
💻 CPP
字号:
#include <iostream.h>
#include <fstream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char filename[255];
char buffer[30000]="",*filebuf=NULL;
int  filesize=0,m,n;
char method;

/*
 * 函数功能:
 *     打开一个指定的文件,然后将得到的文件数据显示在屏幕上。
 * 函数输入:
 *     int m,int n:指明所要显示的行数返回,即表明函数将显示从第m行至第n行的数据。
 */
void Display(int m,int n)
{
	unsigned char s[255];
	ifstream filein(filename);
	// 跳过前面的m-1行
	for (int i=0;i<m-1;i++)
		filein.getline(s,255);
	// 显示指定的行
	for (i=m;i<=n;i++)
	{
		filein.getline(s,255);
		cout<<s<<endl;
		if(filein.eof())
			break;
	}
	filein.close();
}

/*
 * 函数功能:
 *     将输入的内容保存在buffer中
 */
void BufferInput()
{
	char c;
	int i=0;
	strset(buffer,'\0');
	// 保存插入的内容
	while ((c=getchar())!='\n')
		buffer[i++]=c;
	buffer[i] = '\n';
}

/*
 * 函数功能:
 *     保存文件指定函数范围的数据。函数将按指定的参数保存文件的数据,至于保存参数的说明,前面有说明。
 */
void Save(int m,int n)
{
	char s[255];
	long pos;
	int i=0, j=0, k;

	FILE *fd=fopen(filename,"r+");;
	switch (method)
	{
    case 'i':
		// 跳过前面m-1行
		for (i=0;i<m;i++)
			fgets(s,255,fd);
		// 得到文件指针的当前位置
		pos=ftell(fd);
		// 将后面的文件内容存入filebuf
		i=0;
		while (!feof(fd))
			filebuf[i++]=fgetc(fd);
		filebuf[i-1]='\0';
		// 在插入位置加入内容
		fseek(fd,pos,SEEK_SET);
		fprintf(fd,buffer);	
		// 加入原来后面的内容
		fprintf(fd,filebuf);	
		break;
    case 'd':
		// 将文件内容除删去行以外的内容存入filebuf
		while (j!=m-1&&!feof(fd))
		{
			char c=fgetc(fd);
			filebuf[i++]=c;
			if (c=='\n')
				j++;
		}
		for (k=0;k<n-m+1;k++)
			fgets(s,255,fd);
		while (!feof(fd))
			filebuf[i++]=fgetc(fd);
		filebuf[i-1]='\0';
		fclose(fd);
		// 将filebuf的内容写入文件
		fd=fopen(filename,"w");
		fprintf(fd,filebuf);
		break;
    case 'r':
		memset(filebuf, 0, filesize+1);
		i = 0;
		// 将替换后的文件内容存入filebuf
		while (j!=m-1&&!feof(fd))
		{
			char c=fgetc(fd);
			filebuf[i++]=c;
			if (c=='\n')
				j++;
		}
		fgets(s,255,fd);
		strcat(&filebuf[i],buffer);
		i+=strlen(buffer);
		while (!feof(fd))
			filebuf[i++]=fgetc(fd);
		printf("%s",filebuf);
		filebuf[i-1]='\0';
		fclose(fd);
		// 将filebuf的内容写入文件
		fd=fopen(filename,"w");
		fprintf(fd,filebuf);
		break;
	}
	fclose(fd);
}

/*
 * 函数功能:
 *     该函数将按用户指定的参数对文件数据进行处理。
 */
void PrintInterface()
{
	char command[10]="";
	char *ptr=command;
	
	// 打印提示信息
	cout <<endl;
	cout <<"L m,n --display m to n" <<endl;
	cout <<"I m   --insert after m" <<endl;
	cout <<"D m,n --delete m to n"  <<endl;
	cout <<"R m   --replace m"		<<endl;
	cout <<"S     --save"			<<endl;
	cout <<"X     --save and exit"  <<endl;
	cout <<"Q     --quit and exit"  <<endl;
	cout <<endl;

	// 提示用户输入命令
	cout <<"Please input:";
	flush(cout);

	// 得到用户输入的命令,并根据输入进行处理
	gets(command);
	switch (command[0])
	{
    case 'L':
	case 'l':
		if (command[0]=='l')
			sscanf(command,"l %d,%d",&m,&n);
		else
			sscanf(command,"L %d,%d",&m,&n);
		Display(m,n);
		method='l';
		PrintInterface();
		break;
    case 'I':
	case 'i':
		if (command[0]=='i')
			sscanf(command,"i %d",&m);
		else
			sscanf(command,"I %d",&m);
		method='i';
		BufferInput();
		PrintInterface();
		break;
    case 'D':
	case 'd':
		if (command[0]=='D')
			sscanf(command,"D %d,%d",&m,&n);
		else
			sscanf(command,"d %d,%d",&m,&n);
		method='d';
		PrintInterface();
		break;
    case 'R':
	case 'r':
		if (command[0]=='R')
			sscanf(command,"R %d",&m);
		else
			sscanf(command,"r %d",&m);
		method='r';
		BufferInput();
		PrintInterface();
		break;
	case 'S':
	case 's':
		Save(m,n);
		PrintInterface();
		break;
    case 'X':
	case 'x':
		Save(m,n);
    case 'Q':
	case 'q':
		exit(0);
	}
}

/*
 * 主函数
 */
void main()
{
	cout<<"Please Input Filename:";
	cin>>filename;
	
	// 检查文件制定的文件是否可以打开
	ifstream filein(filename);
	if (!filein)
	{
		cout<<"Can't open file!"<<endl;
		exit(0);
	}
	filein.close();
	
	// 得到文件大小
	FILE *fd;
	fd=fopen(filename,"r+");
	while (!feof(fd))
	{
		fgetc(fd);
		filesize++;
	}
	fclose(fd);

	filebuf=new char[filesize+1];
	strset(filebuf,'\0');

	// 调用函数PrintInterface完成各项功能
	PrintInterface();
	
	delete []filebuf;
}

⌨️ 快捷键说明

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