Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C程序中使用系统(“清除”)命令时出现奇怪的输出_C_Unix_Gcc - Fatal编程技术网

在C程序中使用系统(“清除”)命令时出现奇怪的输出

在C程序中使用系统(“清除”)命令时出现奇怪的输出,c,unix,gcc,C,Unix,Gcc,我有以下代码 #include <stdlib.h> #include <stdio.h> #include <string.h> #include <time.h> #include <stdbool.h> #define dimensions 5 int RandomNumInRange(int M, int N) { return M + rand() / (RAND_MAX / (N - M + 1) + 1); }

我有以下代码

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>

#define dimensions 5

int RandomNumInRange(int M, int N)
{
    return M + rand() / (RAND_MAX / (N - M + 1) + 1);
}

char ** CreateWorld(int dim)
{
    int i,j;
    char **world = malloc(dim *sizeof(char*));

    for(i=0;i<dim;i++)
    world[i]=malloc(dim*sizeof(char));

for(i=0;i<dim;i++)
    for(j=0;j<dim;j++)
        world[i][j]=42;

return world;
}


void CreateCastle(char **world)
{

//assuming world is big enough
//to hold a match of 2
int randRow,randCol;

//1 to dimension -2 so we can spawn a 3x3 castle
    randRow = RandomNumInRange(1,dimensions-2);
    randCol = RandomNumInRange(1,dimensions-2);

printf("position: %d %d\n", randRow, randCol);
world[randRow][randCol]='c';
//fill the rest so castle is 3x3
//assuming there is enough space for that
world[randRow-1][randCol-1]=35;
world[randRow-1][randCol]=35;
world[randRow-1][randCol+1]=35;
world[randRow][randCol-1]=35;
world[randRow][randCol+1]=35;
world[randRow+1][randCol-1]=35;
world[randRow+1][randCol]=35;
world[randRow+1][randCol+1]=35;

}


void DisplayWorld(char** world)
{
int i,j;
for(i=0;i<dimensions;i++)
{
    for(j=0;j<dimensions;j++)
    {
        printf("%c",world[i][j]);
    }
    printf("\n");
}
}

int main(void){

system("clear");


int i,j;
srand (time(NULL));


char **world = CreateWorld(dimensions);
DisplayWorld(world);
CreateCastle(world);

printf("Castle Positions:\n");
DisplayWorld(world);

//free allocated memory
free(world);

//3 star strats

char ***world1 = malloc(3 *sizeof(char**));

for(i=0;i<3;i++)
    world1[i]=malloc(3*sizeof(char*));

for(i=0;i<3;i++)
    for(j=0;j<3;j++)
        world1[i][j]="\u254B";

for(i=0;i<3;i++){
    for(j=0;j<3;j++)
        printf("%s",world1[i][j]);
puts("");
}
free(world1);
//end
return 0 ;
}
如果我使用systemclear命令,我会得到一条由[3;J]组成的行 如果我再次运行程序,我得到相同的胡言乱语,那么许多空白的新行,然后是预期的输出。如果我把StAcLeCar命令放在注释中,那么[3;j和空白换行符都不显示,输出预期]。
编辑:似乎错误不在代码中,而是在我系统上的终端未设置的方式中。谢谢大家的输入,我现在肯定有很多有趣的东西要读和学习。

您的clear命令从发送的代码似乎与Gnome终端模拟器不兼容,我相信这就是您想要的将使用

清除控制台的正常控制代码是CSI H CSI J。CSI是控制序列初始值设定项:一个转义字符\033后跟一个[。CSI H将光标发送到起始位置,CSI J从光标位置清除到屏幕的末尾。您也可以使用CSI 2 J清除整个屏幕

<>在Linux控制台和一些终端仿真器上,您可以使用CSI 3 J来清除整个屏幕和回滚。我认为这样做是不友好的,并且安装在我的系统上的清除命令没有。

CSI序列通常可以包含分号来分隔数值参数。但是,J命令不接受多个数值参数,而且分号似乎会导致Gnome终端无法识别控制序列。无论如何,我认为Gnome终端不支持CSI 3 J

clear命令通常使用terminfo数据库为终端查找正确的控制序列。它通过使用TERM环境变量的值来标识终端,这表明您必须为该变量设置错误的值。尝试设置export TERM=xterm,看看是否得到不同的结果。如果这样做有效,您将e来找出LinuxMint在哪里配置环境变量并修复它


总的来说,您不需要使用systemclear来清除屏幕;对于这样一个简单的任务来说,这完全是太多的开销。您最好使用ncurses包中的TPUT。但是,这也使用terminfo数据库,因此在任何情况下都必须修复术语设置。

不要使用systemclear;它不是标准的c,而是h事实上,与c无关,它与c的唯一关系是执行外部命令的系统函数。您不需要systemclear,它只在非常特定的系统上工作。此外,您释放了world,但内存泄漏是因为您不释放world中分配和存储的指针,您只需在其中迭代就像你分配了它们,使用free一样,你需要和你拥有的malloc一样多的免费调用。最后,一定要检查malloc的返回值。@IharobAlAsimi,我同意systemclear可能不合适,而clear部分与C无关。不过,它对我来说在CentOS 7上工作非常好,我希望它能在中工作还有很多其他地方。∑∑πύροςΓούλας,如果您试图在屏幕上以文本模式绘制,而不是仅仅输出文本流,那么我强烈建议您选择并使用适合此目的的库。这种情况下的长时间待机时间是一个或另一个版本,但还有其他选择。您使用的是哪种终端窗口?是gnome终端、Mac终端还是Windows上的cmd窗口?谢谢您详细而有用的回答。这里有很多我不知道的事情。export TERM=xterm不会改变上述行为。∑πρρροςλας这很有趣。如果键入tput clear | hd,您会看到什么?我会的预计输出为1b 5b 48 1b 5b 32 4ait为00000000 1b 5b 33 3b 4a 1b 5b 48 1b 5b 32 4a |[3;J.[H.[2J |[2J | 0000000如果运行TERM=xterm tput clear | hd?它显示00000000 1b 5b 48 1b 5b 32 4a |[H.[2J | 0000000 7