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

📄 textview.cpp

📁 我自己写的一个文本阅读器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TEXTVIEW.cpp : Defines the entry point for the console application.
//

	#include "stdafx.h"
	#include <stdio.h>
	#include <stdlib.h>
	#include <malloc.h>
	#include <process.h> 
	#include <assert.h>
    #include <conio.h>
    #include <string.h>

#include "ps_mem.h"

	FILE *fs = NULL;
	char *p[1000000] = {0};
	char filename[100] = {0};
	char ch = 0;
	int startline = 0;
	int currentline = 0;
	int all_page = 0;
	int all_line = 0;
	int press = 1;
	int prev = 1;
	int lable = 0;
	int currentline_EOF = 0;
	int string_length = 0;
	int b = 0;
	int a = 0;
	

	
	
	//当前已申请的内存块号

	void View(char*[],int &,int&);
	int View_all(FILE*);
	char* Ram_malloc(int,int,FILE*,int&,int **[]);
	
	int main(int argc, char* argv[])
	{
		char *bb;
		bb = (char *)psMalloc(100);
loop:
		puts("请输入要阅读的文件名称");
		gets(filename);

		if (filename[0] == NULL)
		{
			goto loop;
		}

		if ((fs = fopen(filename,"r")) == NULL)
		{
			printf("该文件不存在");
			goto loop;
		}

		printf("此文本文件的行数为%d行\n",all_line = View_all(fs) + 1);
		if (all_line % 20  == 0)
		{
			printf("此文本文件的页数为%d页\n",all_page = all_line / 20);
		} 
		else
		{
			printf("此文本文件的页数为%d页\n",all_page = (all_line / 20 + 1));
		}

		rewind(fs);
		
		    
		ch = fgetc(fs);
		for (int i = 0; ((i < 128) && (ch != EOF)); i++)
		{
			p[i] = (char*)psMalloc(80 * sizeof(char)+1);
			if (p[i] == NULL)
			{
				printf("分配内存失败");
			}
			for (int j = 0; (j < 80); j++)
			{
				*(p[i]+j) = ch;
				if (ch == 10)
				{
					*(p[i]+j+1) = '\0';
					j = 79;
				}
				ch = fgetc(fs);
				*(p[i]+j+1) = '\0';
				if(ch == EOF)
				{
					*(p[i]+j+1) = EOF;
					*(p[i]+j+2) = '\0';
					j = 79;
				}
			}
		}
		View(p,startline,currentline);

	




		puts("请输入需要键入的操作:【Page Up】【Page Down】【↑】【↓】");
		puts("双击【ESC】可直接退出!");
loop1:
		press = getch();
		if (press == 224)
			press =getch();
		else if(press == 27)
		{
			if (lable == 0)
			{
				for (int w = 0; w <= 127; w++)
				{
					psFree(p[(startline / 128) * 128 + w]);
				}
			}
			else
			{
				for (int w = 0; w <= 255; w++)
				{
					psFree(p[(startline / 128) * 128 + w]);
				}
			}
			
			
			ps_memlist_DBGMemoryLeak();
			getch();
			exit(0);
		}
		else
            goto loop1;

		

		switch( press )
		{
		    case 72:            //up

			if(startline == 0)
				;
			else
			{
				system("cls");
				if(lable == 0)
				{
					if(((currentline - 1) / 128 + 1) == ((startline - 1) / 128 + 1))
					{
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline - 1 + k] != NULL)
							{
								printf("%s",p[startline - 1 + k]);
							}
						}
						currentline -= 1;
						startline -= 1;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
					}
					else
					{
						rewind(fs);
						for(int m = 0; m < (((startline / 128) - 1) * 128); m++)
						{
							for(int n = 0; n < 80; n++)
							{
								ch = fgetc(fs);
								if(ch == 10)
								{
									n = 80;
								}
							}
						}                   //为STARTLINE的前128行分配空间

						ch = fgetc(fs);

						for (int l = 128; l >= 1; l--)
						{
							p[startline - l] = (char*)psMalloc(80 * sizeof(char)+1);
							if (p[startline - l] == NULL)
							{
								printf("分配内存失败");
							}
							for (int g = 0; (g < 80); g++)
							{
								*(p[startline - l]+g) = ch;
								if (ch == 10)
								{
									*(p[startline - l]+g+1) = '\0';
									g=79;
								}
								ch = fgetc(fs);
								*(p[startline - l]+g+1) = '\0';
							}
						}                           //为STARTLINE的前128行分配赋值
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline - 1 + k] != NULL)
							{
								printf("%s",p[startline - 1 + k]);
							}
						}
						currentline -= 1;
						startline -= 1;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
						lable = 1;
					}
				}
				else
				{
					if(((currentline - 1) / 128 + 1) == ((startline - 1) / 128 + 1))
					{
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline - 1 + k] != NULL)
							{
								printf("%s",p[startline - 1 + k]);
							}
						}
						for (k = 0; k < 128; k++)
						{
							psFree(p[currentline + k]);
						}
						currentline -= 1;
						startline -= 1;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
						lable = 0;
					}              //由于UP之后不再存在跨界现象,故释放无用内存,减小占用
					else
					{
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline - 1 + k] != NULL)
							{
								printf("%s",p[startline - 1 + k]);
							}
						}
						currentline -= 1;
						startline -= 1;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
					}               //up之后仍然跨界,故不释放内存,直接读取
				}
			}

			break;

		case 80:            //down
			if(p[currentline] == NULL)
				currentline_EOF = 1;
			else
				currentline_EOF = 0;
		                              //检查当前行是否含有EOF值
			if(currentline_EOF == 1)
			   	;
			else
			{
				system("cls");
				if(lable == 0)
				{
					if(((currentline + 1) / 128 + 1) == ((startline + 1) / 128 + 1))
					{
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline + 1 + k] != NULL)
							{
								printf("%s",p[startline + 1 + k]);
							}
						
						}
						currentline += 1;
						startline += 1;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
					}
					else
					{
						rewind(fs);
						for(int m = 0; m < (((startline / 128) + 1) * 128); m++)
						{
							for(int n = 0; n < 80 ; n++)
							{
								ch = fgetc(fs);
								if(ch == 10)
								{
									n = 80;
								}
							}
						}                   //为STARTLINE的后128行分配空间
						
						ch = fgetc(fs);
					
						for (int l = 1; ((l <= 128) && (ch != EOF)); l++)
						{
							p[startline + 19 + l] = (char*)psMalloc(80 * sizeof(char)+1);
							if (p[startline +19 + l] == NULL)
							{
								printf("分配内存失败");
							}
							for (int g = 0; (g < 80); g++)
							{
								*(p[startline + 19 + l]+g) = ch;
								if (ch == 10)
								{
									*(p[startline + 19 + l]+g+1) = '\0';
									g = 79;
								}
								ch = fgetc(fs);
								*(p[startline + 19 + l]+g+1) = '\0';
								if (ch == EOF)
								{
									*(p[startline + 19 + l]+g+1) = EOF;
									*(p[startline + 19 + l]+g+2) = '\0';
									g = 79;
								}
							}
						}                           //为STARTLINE的后128行分配赋值
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline + 1 + k] != NULL)
							{
								printf("%s",p[startline + 1 + k]);
							}
						}
						currentline += 1;
						startline += 1;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
						lable = 1;
					}
				}
				else
				{
					if(((currentline + 1) / 128 + 1) == ((startline + 1) / 128 + 1))
					{
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline + 1 + k] != NULL)
							{
								printf("%s",p[startline + 1 + k]);
							}
						}
						for (int i =0; i < 128; i++)
						{
							psFree(p[startline - i]);
						}
						currentline += 1;
						startline += 1;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
						lable = 0;
					}
					else
					{
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline + 1 + k] != NULL)
							{
								printf("%s",p[startline + 1 + k]);
							}
						}
						currentline += 1;
						startline += 1;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
					}
				}
			}

			break;

		case 73:            //page up			
			if(startline == 0)
				;
            else
			{
				system("cls");
				if((startline < 20) && (startline > 0))
				{
					for (int k = 0; k <= 19; k++)
					{
						if (p[k] != NULL)
						{
							printf("%s",p[k]);
						}
					}
					startline = 0;
					currentline = 19;
					printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
				}
				else
				{
					if (lable == 0)
					{
						if(((currentline - 20) / 128 + 1) == ((startline - 20) / 128 + 1))
						{
							if ((startline % 128 == 0) && (startline % 20 == 0))                     //特殊情况翻页还是LABLE == 0
							{
								rewind(fs);
								for(int m = 0; m < (((startline / 128) - 1) * 128); m++)
								{
									for(int n = 0; (n < 80); n++)
									{
										ch = fgetc(fs);
										if (ch == 10)
										{
											n = 80;
										}
									}

⌨️ 快捷键说明

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