gpt4 book ai didi

C 程序 - 自定义文本编辑器程序

转载 作者:行者123 更新时间:2023-11-30 18:01:53 24 4
gpt4 key购买 nike

我正在为类(class)构建一个程序,该程序应该充当非常基本的基于文​​本的文本编辑器。它有 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);
}
  1. 如果我想发送用户从另一个函数输入的 get_line 输入,我可以将 get_line stdin 发送到识别屏幕上键入的用户输入?或者我是否必须使用某种形式的 fgets 来向其发送信息?

  2. 如果应该允许用户通过用回车键分隔多行(也称为 \n),并且应该允许按 CTRL+D 要继续执行该函数,如何告诉应用程序使 CTRL+D 成为 EOF?

  3. 我的 get_line 函数接收整个文件,并输出一行。我被指示使用多次调用 get_line 来获取多行文件并将每行发送到各自的堆栈条目中。我如何告诉应用程序“这是同一个文件,但我希望您现在检查下一行而不是之前输出的行”?我假设一旦我弄清楚了这一点,我就可以将相同的逻辑应用于用户即时输入的输入。

我被告知get_line函数已经完成,所以我觉得在我会调用get_line的地方(例如在read_file >),我需要通过使 read_file 读取到 \n 的位置来控制一次发送到 get_line 的数量遇到,将其更改为行尾符号(又名 '\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;
}

关于C 程序 - 自定义文本编辑器程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9560672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com