[英]C Program - Custom Text Editor Program
我正在为类构建一个程序,该程序应该用作非常基本的基于文本的文本编辑器。 它有9个可以传递给它的命令,每个命令导致一个不同的命令,并且我们使用双向链表来管理可以添加,插入,删除和浏览的文本行。 我已经获得了一些可以使用的功能,尽管我认为大多数问题都更具概念性,但我将提供这些给定的功能作为背景信息的基础:
// Function: get_line
// Reads a line (of arbitrary length) from an input source (stdin or file)
// and returns a pointer to the array that stores the line read
//
char *get_line(FILE *f)
{
int size = 80; // initial size of memory block allocated
char *linePtr = malloc(size); // allocate memory block
if (linePtr != NULL) { // if allocation successful
int c = EOF;
// read a line of text and replace newline character
// with a string terminator character
int i = 0;
while ((c = getc(f)) != '\n' && c != EOF) {
linePtr[i++] = (char) c;
if (i == size) {
size *= 2;
linePtr = realloc(linePtr, size);
}
}
linePtr[i] = '\0';
// if end-of-file before any characters were read,
// release the whole buffer
if (c == EOF && i == 0) {
free(linePtr);
linePtr = NULL;
} else {
// release unused portion of memory
linePtr = realloc(linePtr, i+1);
}
}
return linePtr;
}
我自定义的“追加”功能:
//
// Function: append_line
// Inserts a line after the current line (or node) in the linked-list
//
void append_line(char *t)
{
line *new_stack;
line *tmp;
new_stack = malloc(sizeof(line));
tmp = malloc(sizeof(line));
if((new_stack == NULL) || (tmp == NULL)) {
fprintf(stderr, "Insufficient memory to allocate. Closing application.\n");
exit(1);
}
if(current_line == NULL) {
if(head == NULL) {
head = new_stack;
current_line = new_stack;
new_stack->prev = NULL;
new_stack->next = NULL;
}
}
else {
tmp = current_line->next;
current_line->next = new_stack->prev;
new_stack->next = tmp;
current_line = new_stack;
}
new_stack->text = t;
free(tmp);
}
这是我的read_file函数,它实际上还没有执行任何操作,但是我不确定是否可以正确地将心态投入到此函数的创建中:
// Function: read_file
// Reads text from the specified file and calls append_line to insert lines
// into the linked-list. It returns the number of lines read.
//
int read_file(char *filename)
{
char * temp, no_command;
temp = strtok(filename, " ");
while(temp != NULL) {
no_command = temp;
temp = strtok (NULL, " ");
}
/* By doing this --^, I hope it will set temp to the actual
// file name after tokenization and completely ignore
// the command that comes with filename */
FILE *fin;
int counter = 0;
fin = fopen(no_command, "r");
if(fin == NULL) {
printf("You have entered a file that does not exist.");
exit(0);
}
get_line(fin);
fclose(fin);
}
如果我想发送用户从另一个函数输入的get_line
输入,是否可以发送get_line
stdin
使其识别在屏幕上键入的用户输入? 还是我必须利用某种形式的fgets
将信息发送给它?
如果应该允许用户使用回车键(aka \\n
)将它们分开输入多行,并且希望允许用户按CTRL + D继续该功能,那么如何告诉应用程序进行CTRL + d的EOF?
我的get_line
函数接收整个文件,并输出一行。 我被指示使用get_line
的多个调用来获取多行文件,并将每一行发送到各自的堆栈条目中。 我如何告诉应用程序“这里是相同的文件,但是我希望您现在检查下一行而不是以前输出的那一行”? 我假设一旦弄清楚了这一点,便可以将相同的逻辑应用于用户即时输入的输入。
我被告知get_line
函数是完整的,所以我感觉在我将调用get_line
的地方(例如在read_file
),我需要通过使read_file
读至get_line
来控制一次发送到get_line
的数量。遇到\\n
点,将其更改为行尾符号(aka '\\0'
),然后将其发送到get_line
,然后以某种方式使read_file在该点之后继续执行相同的操作,直到达到EOF。 但是我也觉得很多功能也都在get_line
函数中...所以我想我对给定函数的实现感到困惑。
将来我可能还会有更多问题,但就目前而言,我相信这个最初的问题已经解决了很多。 我希望弄清楚这些事情,其余的事情只需在我的脑海中点击就位。 感谢您的时间!
append_line
函数中还有两个错误,例如,在初始化之前,您使用new_stack->prev
。
这是我的看法:
void append_line(char *t)
{
/* Allocate and clear (i.e. set all to 0) */
line *new_stack = calloc(1, sizeof(line));
if(current_line == NULL) {
if(head == NULL) {
head = current_line = new_stack;
}
}
else {
new_stack->next = current_line;
new_stack->prev = current_line->prev;
if (current_line->prev)
current_line->prev->next = new_stack;
else
head = new_stack; /* No previous node, means current_line is at head */
current_line = new_stack;
}
new_stack->text = t;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.