gpt4 book ai didi

c - C语言如何在屏幕中间打印一段文字?

转载 作者:太空宇宙 更新时间:2023-11-04 02:00:03 24 4
gpt4 key购买 nike

我确实设法到达中心的水平线(行),但不是垂直的。请帮助,我仍然是一个菜鸟,所以如果你能解释你的代码,那将不胜感激。

谢谢

#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>

int main()
{

struct winsize w;
ioctl(0, TIOCGWINSZ, &w);

int columns = w.ws_col;
int rows = w.ws_row;


//printf("Lines : %d\n", rows);
//printf("Columns ; %d\n", columns);

char *string = "Hello World";
int stringLength = strlen(string) / 2;



printf("%*s\n", columns / 2 + stringLength, string );
//getchar(); //Pause program

return 0;
}

最佳答案

不知道为什么这会被否决。似乎是一个完全有效的问题。

您已经解决了水平问题。干得好!

如果您在 UNIX 环境中(尤其是 Bash),您可以查询环境变量 $LINES$COLUMNS 来找出屏幕的长度和宽度是。

关于c - C语言如何在屏幕中间打印一段文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28443236/

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