-1
I’m having some trouble making a text editor there will some of them:
1 - I don’t know how to delete a line;
2 - I can’t move with mouse arrows up and edit a line of text and save in sequence;
3 - Put options as ctrl+s
to save ctrl+f
to look for;
This is my code, if anyone can help me I’d appreciate it, I’m a beginner in C.
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#define praCima 72
#define praCima 72
#define praBaixo 80
#define enter 13
#define esc 27
typedef struct reg no;
struct reg{
char infoLinha[100];
no *prox, *ant;
};
void cria_lista (no *lista)
{
lista = NULL;
}
void exibeTexto(no *lista, int sel) {
no *atual;
atual = lista;
int cont = 0;
while(atual != NULL) {
if (sel >= 0)
printf("%c %s\n", ((sel == cont) ? '>' : ' '), atual->infoLinha);
else
printf("%s%s", ((cont == 0) ? " " : "\n "), atual->infoLinha);
cont++;
atual = atual->prox;
}
}
no* Nelemento(no *lista, int n, int tam) {
int cont = 0;
no *elemento;
elemento = lista;
if (n > tam-1)
return NULL;
while (cont < n) {
elemento = elemento->prox;
cont++;
}
return elemento;
}
void escreve() {
no *lista;
int sel = 0;
int tam = 0;
cria_lista(lista);
lista = (no*)malloc(sizeof(no));
lista->ant = lista->prox = NULL;
tam++;
strcpy(lista->infoLinha, "");
no* atual;
atual = lista;
int sai = 0;
char op = 0;
do {
system("cls");
exibeTexto(lista, sel);
printf("\n\n\t <ENTER> SELECIONAR | <ESC> SAIR");
op = getch();
switch(op) {
case enter:
system("cls");
fflush(stdin);
atual = Nelemento(lista, sel, tam);
no *p;
p = (no*)malloc(sizeof(no));
p->prox = atual;
p->ant = atual->ant;
gets(p->infoLinha);
atual->ant = p;
if (p->ant != NULL)
p->ant->prox = p;
if(p->prox == lista)
lista = p;
tam++;
sel = tam-1;
break;
case esc:
sai = 1;
break;
case praCima:
if (sel > 0)
sel--;
break;
case praBaixo:
if (sel < tam-1)
sel++;
break;
}
if (sai)
break;
} while(op != '2');
}
void escreveSalva() {
no *lista;
FILE *arquivo;
if ((arquivo = fopen("linhas.txt", "r+b")) == NULL) {
if ((arquivo = fopen("linhas.txt", "wb")) == NULL) {
printf ("\nErro da abertura do arquivo.\n\n");
getch();
return;
}
}
int sel = 0;
int tam = 0;
cria_lista(lista);
lista = (no*)malloc(sizeof(no));
lista->ant = lista->prox = NULL;
tam++;
strcpy(lista->infoLinha, "");
no* atual;
atual = lista;
int sai = 0;
char op = 0;
do {
system("cls");
exibeTexto(lista, sel);
printf("\n\n\t <ENTER> SELECIONAR | <ESC> SAIR");
op = getch();
switch(op) {
case enter:
system("cls");
fflush(stdin);
atual = Nelemento(lista, sel, tam);
no *p;
p = (no*)malloc(sizeof(no));
p->prox = atual;
p->ant = atual->ant;
gets(p->infoLinha);
atual->ant = p;
if (p->ant != NULL)
p->ant->prox = p;
if(p->prox == lista)
lista = p;
tam++;
sel = tam-1;
if(p->infoLinha != NULL)
{
fprintf(arquivo, "%s\n", p->infoLinha);
}
break;
case esc:
sai = 1;
break;
case praCima:
if (sel > 0)
sel--;
break;
case praBaixo:
if (sel < tam-1)
sel++;
break;
}
if (sai)
break;
} while(op != '2');
}
void menu()
{
char tc;
int i,sai=0,sel=0;
int tamMenu=2;
while (!sai)
{
system("cls");
printf("Selecione uma das opcoes:\n\n\n");
char menu[2][50]={"Escrever informacoes e salvar em arquivo","Apenas escrever informacoes sem salvar"};
for (i=0;i<tamMenu;i++)
{
if (sel==i)
{
printf("\t > ");
}
else printf("\t ");
printf("%s\n",menu[i]);
}
printf("\n\n\t <ENTER> SELECIONAR | <ESC> SAIR");
tc = getch();
switch(tc)
{
case praCima: if (sel>0) { sel--; } else sel=tamMenu-1;
break;
case praBaixo: if (sel<tamMenu-1) { sel++; } else sel=0;
break;
case enter:
switch(sel)
{
case 0:
{
escreveSalva();
}
break;
case 1:
{
escreve();
}
break;
}
break;
case esc: sai=1;
break;
}
}
}
int main(){
menu();
return 0;
}
Hello, Giovanni. Welcome to Stack Overflow in English. Your question, as it stands, is very wide. Try to break it into more specific (and smaller) questions and you’ll have a better chance of finding help.
– Pablo Almeida
Is it C or C++? Not always what you do in one serves another.
– user28595
@Diegofelipe Even if it is c++ it seems that Giovanni is not using any language specifics.
– krystalgamer
Do you know anything about data structures? Inverted lists, B-Tree, those things?
– Intruso
@Intruder yes, yes I revoked the problem of erasing and moving the arrow but I can’t edit that list and only delete the last element of the list, another thing I couldn’t find was how to read Ctrl+some key
– Giovanni
@Giovanni Qual compiler and S.O. (name and version) you are using to develop?
– Gomiero