Tag: stdin

C – getchar()在循环中?

我如何在循环中使用getchar()? 我现在有… for (p=0; p<n_players; p++) { … fflush(stdin); getchar(); } 但它不起作用……如果n_players为3,它只在最后执行getchar 2次… for (p=0; p<n_players; p++) { blank_start(); ascii_art_title(); printf("%s, tocca a te…\n",player_info[p].player_name); srand(time(NULL)); random_speed = MIN_WHEEL_SPEED + rand()%MAX_WHEEL_SPEED; move_wheel_pointer(random_speed, &pointer); if (player_points(&wheel[pointer]) == 0){ player_info[p].points = wheel[pointer]; } else { player_info[p].points = 0; } printf("\nGuadagni %d punti…\n",player_info[p].points); if (p<(n_players-1)) { printf("\nOra tocca a […]

进入没有malloc的char * str,没有segfault?

我刚刚使用Cygwin的gcc编译了这个C程序: #include void main (){ char *str; gets(str); printf(“%s”,str); } 抛弃gets的弃用已经过时了,这应该打破,因为我没有为str分配任何内存,但它甚至可以用很长的输入。 例如,如果我设置了char str [16],它会在超过分配的长度后中断几个字符。 为什么我没有得到分段错误?

如何通过只输入一个EOF来结束scanf

我在讨论这个问题。 我正在使用while循环来扫描数字串,需要结束扫描并开始继续我的程序的其余部分。 我只是想不通,如何刷新标准输入或做任何事情都不按两次Ctrl + D. 我只需要发送一次EOF来告诉我的循环结束。 while (! feof (stdin)) {status=scanf (“%d”, &array[i]); if ( (status != 1 && status != EOF) ) { printf(“\nWrong input.\n”); return 1;} i++;}

为什么在循环内部,带有%d的scanf()不会等待用户输入,以防它先前收到无效输入?

我正在使用scanf() returns when it gets what is expects or when it doesn’t. What happens is it gets stuck in the scanf() returns when it gets what is expects or when it doesn’t. What happens is it gets stuck in the while()循环中。 据我所知test = scanf(“%d”, &testNum); 如果收到数字则返回1,否则返回0。 我的代码: #include int main(void) { while (1) { int […]

C低级标准输入接受文件名然后将文件内容打印到标准输出

我想通过stdin从用户获取文件名,用open()打开文件并将其分配给文件描述符,然后将该文件的内容打印到stdout。 这是我的代码,但它无法正常工作。 问题: printf(“输入文件名”); 声明永远不会出现 它从不打开文件; 相反,无论用户输入打印到屏幕上,然后打印“无此文件或目录”错误消息并退出程序 程序存在后,我看到在终端提示符前打印的“输入文件名” 码: { printf(“Enter the filename: “); read(STDIN_FILENO, userInput, sizeof(userInput)); if((input_file1 = open(userInput, O_RDONLY)) 0) { if((write(STDOUT_FILENO, buffer, n)) < 0) { perror("failed to write to standard-out"); close(input_file1); exit(1); } } } 安慰: machine{user1}168: ls // to show that the file exists a.out backup file1 machine{user1}170: ./a.out file1 […]

如何为另一个使用stdin输入的函数编写测试函数?

作为大学任务的一部分,我有以下function: int readMenuOption() { /* local declarations */ char option[2]; /* read in 1 char from stdin plus 1 char for string termination character */ readStdin(1 + 1, option); return (int)option[0] <= ASCII_OFFSET ? 0 : (int)option[0] – ASCII_OFFSET; } int readStdin(int limit, char *buffer) { char c; int i = 0; int read = […]

没有fopen()的读文件(C语言)

我正在开展一个学校项目,我们必须在保存在.txt文件中的表上执行一些操作(select,min,max)。 问题是我们不能使用常见的函数,如fopen,fscanf,fclose。 该程序将从命令行启动,如下所示: .\project.exe select parameters <table.txt 你有一些想法如何在不使用fopen的情况下将.txt文件的内容转换为stdin吗? 谢谢。

从C中的命令行捕获可变长度字符串

我到处寻找我的问题的答案,但我还没有找到一个可靠的答案来解决我的问题。 我目前正在用C编写程序,专门针对UNIX命令行(我使用Linux作为我的开发环境,但我希望这个程序尽可能便携)。 现在,我有一个基本的shell,提示用户输入。 然后,用户将输入命令,并相应地处理该命令。 这是我到目前为止的代码: /* Main.c */ int main(int argc, char **argv) { while (TRUE) { display_prompt(); get_command(); } return 0; } /* Main.h */ void get_command() { /* * Reads in a command from the user, outputting the correct response */ int buffer_size = 20; char *command = (char*) malloc(sizeof(char) * buffer_size); if […]

使用stdin和stdout使用Java与外部C程序通信

我要做的是在Java应用程序中启动C程序可执行文件,并允许它们使用stdin和stdout相互通信。 C程序将等待来自Java应用程序的命令并回显它。 我用“gnugo –mode gtp”测试了java代码(gnugo在gtp模式下与stdin和stdout通信)并且它工作正常,但我的C代码不起作用。 任何建议都会非常感激。 C代码 #include #include #include int main(void) { unsigned int byte_read; char *string, *tok; int cmd_id; int len = 64; string = (char *) malloc(len + 1); while (1) { byte_read = getline(&string,&byte_read, stdin); if (byte_read == -1) { printf(“Error reading input\n”); free(string); exit(0); // } else { printf(“Got command: […]

有效的stdin阅读c编程

任何人都可以帮助我优化代码来读取标准输入。 这就是我现在拥有的: unsigned char *msg; size_t msgBytes = 0; size_t inputMsgBuffLen = 1024; if ( (msg = (unsigned char *) malloc(sizeof(unsigned char) * inputMsgBuffLen) ) == NULL ) { quitErr(“Couldn’t allocate memmory!”, EXIT_FAILURE); } for (int c; (c = getchar()) != EOF; msgBytes++) { if (msgBytes >= (inputMsgBuffLen)) { inputMsgBuffLen <<= 1; if ( ( […]