我如何获取文件的光标位置并更改它?

How can i take the cursor position of a file and change it?

我正在用 C 编写一个简单的文本编辑器,我定义了一个名为 node 的结构并创建了一个名为 textbuffer 的链表。我正在尝试为文本编辑器创建一个插入功能。这是到目前为止的代码:

#include <stdio.h>
#include <string.h>

struct node
{
    char statement[40];
    int next;
};
struct node textbuffer[25];



void insert(int line, char* stat)
{
    FILE *file;
    file=fopen("texteditor.txt","a");
    
    if(file!=NULL)
    {
        int i;
        int k;
        
        strcpy(textbuffer[line].statement,stat);
        textbuffer[line].next=line+1;
        fprintf(file,textbuffer[line].statement);
        
        
    }
    else
    {
        printf("File couldn't found.");
    }
    fclose(file);
}


现在我需要获取当前光标在文件中的位置,例如,当输入为“w”时,我需要将光标向上移动或者当输入为“z”时,我需要获取将光标向下并在那里写一些文字。如果我用照片解释:

photo

我尝试使用 wherex() 和 wherey() 方法来做到这一点,但是 conio.h 库在 linux 中不起作用。我必须使用 conio.h" 库中的方法来完成吗?如果是这样,怎么做?或者还有其他方法吗?

您可以使用转义序列进行各种光标操作和更多屏幕操作(插入、删除、清除、select 颜色等)。这可以在 Linux 下使用 ncurses library which does it almost whatever the terminal is. But today, most if not all terminals are supporting XTerm escape sequences which inherit from ANSI, VT100 and VT52. This also works in Windows 10. Have a look at this Wikipedia article 了解更多信息。

你也可以看看这个detailed documentation