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

📄 textview.cpp

📁 我自己写的一个文本阅读器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
								}                   //为STARTLINE的前128行分配空间
								
								ch = fgetc(fs);
								
								for (int l = 0; l <= 127; l++)
								{
									p[((startline / 128 - 1) *128) + l] = (char*)psMalloc(80 * sizeof(char)+1);
									if (p[((startline / 128 -1) *128) + l] == NULL)
									{
										printf("分配内存失败");
									}
									for (int g = 0; (g < 80); g++)
									{
										*(p[((startline / 128 - 1) *128) + l]+g) = ch;
										if (ch == 10)
										{
											*(p[((startline / 128 - 1) *128) + l]+g+1) = '\0';
											g=79;
										}
										ch = fgetc(fs);
										*(p[((startline / 128 - 1) *128) + l]+g+1) = '\0';
									}
								}                           //为STARTLINE的前128行分配赋值
								for (int k = 0; k <= 19; k++)
								{
									if (p[startline - 20 + k] != NULL)
									{
										printf("%s",p[startline - 20 + k]);
									}
								}

								for (int i = 0;i < 128; i++)
								{
									psFree(p[((currentline / 128) * 128) + i]);
								}
								
								currentline -= 20;
								startline -= 20;
							    printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
							} 
							else
							{
								for (int k = 0; k <= 19; k++)
								{
									if (p[startline - 20 + k] != NULL)
									{
										printf("%s",p[startline - 20 + k]);
									}
								}
								currentline -= 20;
								startline -= 20;
								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 = 0; l <= 127; l++)
							{
								p[((startline / 128 - 1) *128) + l] = (char*)psMalloc(80 * sizeof(char)+1);
								if (p[((startline / 128 -1) *128) + l] == NULL)
								{
									printf("分配内存失败");
								}
								for (int g = 0; (g < 80); g++)
								{
									*(p[((startline / 128 - 1) *128) + l]+g) = ch;
									if (ch == 10)
									{
										*(p[((startline / 128 - 1) *128) + l]+g+1) = '\0';
										g=79;
									}
									ch = fgetc(fs);
									*(p[((startline / 128 - 1) *128) + l]+g+1) = '\0';
								}
							}                           //为STARTLINE的前128行分配赋值
							for (int k = 0; k <= 19; k++)
							{
								if (p[startline - 20 + k] != NULL)
								{
									printf("%s",p[startline - 20 + k]);
								}
							}
							currentline -= 20;
							startline -= 20;
							printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
							lable = 1;
						}
					} 
				    else
					{
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline - 20 + k] != NULL)
							{
								printf("%s",p[startline - 20 + k]);
							}
						}

						for (int i = 0;i < 128; i++)
						{
							psFree(p[((currentline / 128) * 128) + i]);
						}
						currentline -= 20;
						startline -= 20;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
						lable =0;
					}
				}
			}
		    break;

		case 81:            //page 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 + 20) / 128 + 1) == (startline + 20) / 128 + 1)
					{
						if (((startline +20) % 128 == 0) && ((startline + 20)% 20 == 0))  //出现极端情况如640之类LABLE =0 翻页还是=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;
									}
								}
							}                   //为STARTLINE的后128行分配空间
							
							ch = fgetc(fs);
							
							for (int l = 0; ((l <= 127) && (ch != EOF)); l++)
							{
								p[(((startline / 128) + 1) * 128) + l] = (char*)psMalloc(80 * sizeof(char)+1);
								if (p[(((startline / 128) + 1) * 128) + l] == NULL)
								{
									printf("分配内存失败");
								}
								for (int g = 0; (g < 80); g++)
								{
									*(p[(((startline / 128) + 1) * 128) + l]+g) = ch;
									if (ch == 10)
									{
										*(p[(((startline / 128) + 1) * 128) + l]+g+1) = '\0';
										g = 79;
									}
									ch = fgetc(fs);
									*(p[(((startline / 128) + 1) * 128) + l]+g+1) = '\0';
									if (ch == EOF)
									{
										*(p[(((startline / 128) + 1) * 128) + l]+g+1) = EOF;
										*(p[(((startline / 128) + 1) * 128) + l]+g+2) = '\0';
										g = 79;
									}
								}
							}                           //为STARTLINE的后128行分配赋值

								for (int k = 0; k <= 19; k++)
								{
									if (p[startline + 20 + k] != NULL)
									{
										printf("%s",p[startline + 20 + k]);
									}
								}

								for (int w = 0; w <= 127; w++)
								{
									psFree(p[(startline / 128) * 128 + w]);
								}

								currentline += 20;
								startline += 20;
								printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);

						}
						else
						{

								for (int k = 0; k <= 19; k++)
								{
									if (p[startline + 20 + k] != NULL)
									{
										printf("%s",p[startline + 20 + k]);
									}
								}
								currentline += 20;
								startline += 20;
								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 = 0; ((l <= 127) && (ch != EOF)); l++)
						{
							p[(((startline / 128) + 1) * 128) + l] = (char*)psMalloc(80 * sizeof(char)+1);
							if (p[(((startline / 128) + 1) * 128) + l] == NULL)
							{
								printf("分配内存失败");
							}
							for (int g = 0; (g < 80); g++)
							{
								*(p[(((startline / 128) + 1) * 128) + l]+g) = ch;
								if (ch == 10)
								{
									*(p[(((startline / 128) + 1) * 128) + l]+g+1) = '\0';
									g = 79;
								}
								ch = fgetc(fs);
								*(p[(((startline / 128) + 1) * 128) + l]+g+1) = '\0';
								if (ch == EOF)
								{
									*(p[(((startline / 128) + 1) * 128) + l]+g+1) = EOF;
									*(p[(((startline / 128) + 1) * 128) + l]+g+2) = '\0';
									g = 79;
								}
							}
						}                           //为STARTLINE的后128行分配赋值

						for (int k = 0; k <= 19; k++)
						{
							if (p[startline + 20 + k] != NULL)
							{
								printf("%s",p[startline + 20 + k]);
							}
						}
						currentline += 20;
						startline += 20;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);

						lable = 1;
					}
				}
				else
				{
						for (int k = 0; k <= 19; k++)
						{
							if (p[startline + 20 + k] != NULL)
							{
								printf("%s",p[startline + 20 + k]);
							}
						}
						for (int w = 0; w <= 127; w++)
						{
							psFree(p[(startline / 128) * 128 + w]);
						}
						currentline += 20;
						startline += 20;
						printf("当前从第%d行读取到第%d行",startline + 1,currentline + 1);
						lable = 0;
				}
			}
		    break;


		default :
		    break;
		}
		goto loop1;
	
	    return 0;
	}


	void View(char *a[], int &startline, int &currentline)
	{
		for(int i = 0; i < 20; i++)
		{
			if (a[startline+i] != NULL)
			{
				printf("%s",a[startline+i]);
			}
			currentline++;
		}
		currentline -= 1;
		printf("\n当前文件已从第%d行读取到第%d行\n",startline + 1,currentline + 1);
	}

	int View_all(FILE *b)                        //计算文件总行数
	{
		char cp = fgetc(b);
		int all_line = 0;
		while (cp != EOF)
		{
			for (int i = 1; i < 81; i++ )
			{
				if (cp == EOF)
					i = 80;
				else if(i == 80)
					all_line++;
				else if(cp == 10)
				{
					i = 81;
					all_line++;
				}
				cp = fgetc(b);
			}
		}
		return all_line;
	}

// 	char* Ram_malloc(int all_line,int currentline,FILE *fs,int &prev,char **a)// b
// 	{
// 		int n = (all_line / 128) + 1;                //need to psMalloc how many memory
// //		char *a[128] = {0};
// 		int m = (currentline / 128 + 1);            //belongs to which RAM_BLOCK
// 		if(m)
// 		{
// 			if (prev == m)
// 				return *a;
// 			else
// 			{
// 				prev = m;
// 				ch = fgetc(fs);
// 				for(int i = 0; ((i < (128 * (m - 1))) && (ch != EOF)); i++)
// 				{
// 					for(int j = 0; ((j < 80) && (ch != 10) && (ch != EOF)); j++)
// 					{
// 						ch = fgetc(fs);
// 					}
// 				}
// 			 //make the file_points fs point to the right place
// 				for (int k = 0; k < 128; k++)
// 				{
// 					*(a + k) = NULL;
// 				}
//              //psFree the *a[]
// 				for (int p = 0; ((p < 128) && (ch != EOF)); p++)
// 				{
// // 					*(a + p) = (char *)psMalloc(80 * sizeof(char)+1);
// // 					if (*(a + p) == NULL)
// // 					{
// // 						printf("分配内存失败");
// // 					}
// 					for (int q = 0; (q < 80); q++)
// 					{
// 						*(*(a + p)+q) = ch;
// 						if (ch == 10)
// 						{
// 							*(*(a + p) + q + 1) = '\0';
// 							q=79;
// 						}
// 						ch = fgetc(fs);
// 						*(*(a + p) + q + 1) = '\0';
// 					}
// 				}
// 			 //write the 10240 bytes to the rams which were malloced
// 				return *a;
// 			}
// 		}
// 		else
// 		{
// 			printf("fatal error");
//             return 0;
// 		}
// 	}
// 



		

⌨️ 快捷键说明

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