C 从stdin读取行,直到EOF,并在测试后打印第一个字符的文本
我希望交互式(我想)从标准输入读取一行,直到EOF,但在每行之后,如果第一行字符是C 从stdin读取行,直到EOF,并在测试后打印第一个字符的文本,c,stdin,C,Stdin,我希望交互式(我想)从标准输入读取一行,直到EOF,但在每行之后,如果第一行字符是“+”,我想打印,然后打印“OK”,否则打印“NOT OK”。我尝试了此代码,但即使我输入的行的第一个字符等于“+”,也会打印出“不正常” int main() { #define BUF_SIZE 1024 char buffer[BUF_SIZE]; size_t contentSize = 1; /* Preallocate space. We could just allo
“+”
,我想打印,然后打印“OK”
,否则打印“NOT OK”
。我尝试了此代码,但即使我输入的行的第一个字符等于“+”
,也会打印出“不正常”
int main()
{
#define BUF_SIZE 1024
char buffer[BUF_SIZE];
size_t contentSize = 1;
/* Preallocate space. We could just allocate one char here,
but that wouldn't be efficient. */
char *content = malloc(sizeof(char) * BUF_SIZE);
if(content == NULL)
{
perror("Failed to allocate content");
exit(1);
}
content[0] = '\0'; // make null-terminated
while(fgets(buffer, BUF_SIZE, stdin))
{
char *old = content;
contentSize += strlen(buffer);
content = realloc(content, contentSize);
if(content == NULL)
{
perror("Failed to reallocate content");
free(old);
exit(2);
}
strcat(content, buffer);
if (content[0]== '+') {
printf("OK\n");
} else {
printf("NOT OK\n");
}
}
if(ferror(stdin))
{
free(content);
perror("Error reading from stdin.");
exit(3);
}
}
您正在将缓冲区连接到内容
strcat(content, buffer);
因此,对于第一次输入,假设“abc”content
将是abc,并且打印不正常。
对于第二个输入,假设“+xyz”content
将是abc+xyz,那么content[0]
的值将始终是“a”,因此它将始终打印不正常
同样,如果您的第一个输入为“+abc”,则它将始终为所有输入打印OK
使用strcpy而不是strcat
strcpy(content, buffer);
要通过fgets()
读取一行,最好将其作为独立函数处理
以下建议代码与OP类似。一个关键区别是测试fgets(buffer)是否读取'\n'
#include <math.h>
#include <stdio.h>
#define BUF_SIZE 10
char *readline_mardon(void) {
char buffer[BUF_SIZE];
size_t contentSize = 1;
char *content = malloc(contentSize);
if (content == NULL) {
perror("Failed to allocate content");
exit(1);
}
content[0] = '\0'; // make null-terminated
while (fgets(buffer, sizeof buffer, stdin)) {
size_t buffer_length = strlen(buffer);
// more idiomatic code
// Assign `content` after successful allocation detected
size_t contentSize_new = contentSize + buffer_length;
printf("%zu <%s>\n", buffer_length, buffer);
char *content_new = realloc(content, contentSize_new);
if (content_new == NULL) {
perror("Failed to reallocate content");
free(content);
exit(2);
}
// memcpy faster than strcat as the end of the first part is known
memcpy(content_new + contentSize - 1, buffer, buffer_length + 1);
content = content_new;
contentSize = contentSize_new;
// look for \n
if (buffer_length > 0 && buffer[buffer_length - 1] == '\n') {
break;
}
}
return content;
}
如果没有读取任何内容或出现输入错误,其他代码可能返回
NULL
。问题是?顺便说一句,你的
键坏了吗?是的,即使contentn中的第一个字符是'+'OT,此代码打印也不正常:你能确保没有输入行长于1023char
s吗?不,我不确定,可能所有代码都坏了。你想分别处理读取输入和解释输入。我对行content[0]='\0'进行注释代码>任何输入行后的脚本优先级都不正常,即使第一个字符为+对不起,无需对此进行注释。我已经编辑了我的评论。执行并确认此操作。我尝试此操作,但即使行有第一个字母+NOT OK正在打印,当输入行少于10个字符时,我在realloc中获得错误
char *s;
while((s = readline_mardon()) != NULL) {
if (s[0]== '+') {
printf("OK\n");
} else {
printf("NOT OK\n");
}
free(s);
}