我正在用 C 编程,我需要知道标准输入的行数。在一定数量的行之后,我还需要向上滚动一行......我使用了 ANSI 转义码(033 [1S),但我丢失了滚动行的内容,我不想这样。
编辑:解释第二点的简单代码
#include <stdio.h>
int main(void) {
printf("one\ntwo\nthree\n");
fputs("\033[1S", stdout);
return 0;
}
我正在用 C 编程,我需要知道标准输入的行数。在一定数量的行之后,我还需要向上滚动一行......我使用了 ANSI 转义码(033 [1S),但我丢失了滚动行的内容,我不想这样。
编辑:解释第二点的简单代码
#include <stdio.h>
int main(void) {
printf("one\ntwo\nthree\n");
fputs("\033[1S", stdout);
return 0;
}
这是 ansi 转义码向下滚动页面到代码表的一个很好的参考。
我相信除了“1S”之外,您可能还需要 \033[1E 才能向下移动到新行。玩弄代码。
我也认为您可以从环境中获取线条/列。
#include <sys/types.h>
#include <sys/ioctl.h>
#include <stdio.h>
int main()
{
struct winsize ws;
ioctl(1, TIOCGWINSZ, &ws);
printf("Columns: %d\tRows: %d\n", ws.ws_col, ws.ws_row);
return 0;
}
看起来你误解了什么是标准输入。
在您的示例中,在评论中:
#include <stdio.h>
int main(void) {
int c; int i = 1;
printf("one\ntwo\nthree\n");
//while((c=fgetc(stdin)) != NULL) {
// comparing it with null is not correct here
// fgetc returns EOF when it encounters the end of the stream/file
// which is why an int is returned instead of a char
while((c=fgetc(stdin)) != EOF) {
if (c=='\n') {
printf("%d\n", i); i++;
}
}
return 0;
}
从命令行调用程序应该输出这个
$ prog
one
two
three
您必须向其发送流或管道以通过标准输入向其提供信息
$ cat myfile | prog
one
two
three
4 # or however many lines are in myfile
stdin 默认为空白。如果您输入它,则在您输入之前不会发送任何内容
这是我从编译上面的代码中看到的:
1 ./eof_testing
one
two
three
jfklksdf #my typing here
1
fjklsdflksjdf #mytyping here
2
fjklsdflksdfjf # my typing here
3
-----添加stty系统调用示例----
#define STDIN_FD 0
#define STDOUT_FD 1
#define CHUNK_SIZE 8
#define QUIT_CHAR (char)4 /* C-D */
int main(){
write(STDOUT_FD,"hi\n",3);
char buff[CHUNK_SIZE];
int r, i;
system("stty -echo raw");
while(r = read(STDIN_FD, &buff, CHUNK_SIZE)){
for(i = 0; i < r; i++){
if(buff[i] == QUIT_CHAR)
goto exit;
}
write(STDOUT_FD, &buff, r);
}
exit:
system("stty echo cooked");
return 0;
}
但是,现在有一系列全新的挑战需要解决,例如键发送 '\r' 字符,因此它不再是换行符,而是返回到行首。这是因为现在字符直接进入程序,行不会以终端在“cooked”模式下发生的“\n”字符终止。
http://starboard.firecrow.com/interface_dev/mle.git/editor/editor.c