fgets 应该在 stdin 结束时停止
fgets should stop when stdin ends
我正在尝试使用 fgets
从 stdin
中读取数据,但是是小块读取。我的问题是我不确定在标准输入结束时 fgets
等于什么。从我的代码来看,它似乎显然不是 NULL
或像 \n
这样的行尾。执行此操作的正确方法是什么?
代码(永远循环,因为 fgets
永远不会等于 NULL
):
int main ()
{
char str1[4];
printf("Enter stuff: ");
while (fgets(str1, sizeof str1, stdin) != NULL &&
fgets(str1, sizeof str1, stdin) != "\n") {
printf("Got here");
}
printf("%s\n",str1);
return 0;
}
改变
while(fgets(str1, sizeof str1, stdin) != NULL && fgets(str1, sizeof str1, stdin) != '\n'){
到
while(fgets(str1, sizeof str1, stdin) != NULL){
或到
while(fgets(str1, sizeof str1, stdin)){
fgets
returns一个char*
。您无法将它与 '\n'
进行比较,后者是 char
。这个函数,遇到EOF
时returnsNULL
。您可以通过按
在 stdin
上模拟 EOF
- CTRL+Z 在 windows
- CTRL+D 在 linux
你可以这样做:
while (fgets(str1, sizeof str1, stdin) != NULL && str1[0] != '\n')
如果 fgets()
读取换行符,它会将其存储在字符串中,如果遇到 EOF
,则 returns NULL
。
这样你得到输入并测试如果 fgets()
首先遇到 EOF
,然后你测试字符串中的第一个字符 (str1[0]
) 看它是否是换行符。
记住 fgets()
returns a char *
所以你可以使用指针符号 *str1
或数组符号 str1[0]
来测试字符串的第一个字符。
// Rajat Sewal
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct island {
char *name;
char *opens;
char *closes;
struct island *next;
} island;
int main()
{
printf("This is an example of linked lists using a file to read the data needed!\n");
printf("to create the structs dynamically!\n\n\n");
// First we need a function called island that creates the new island struct
/********************************************/
island* create(char *name)
{
island *i = malloc(sizeof(island));
// set the fields on the new struct
i->name = strdup(name); // strdup duplicates the string so a NEW copy is used
i->opens = "09:00";
i->closes = "17:00";
i->next = NULL;
return i;
}
/********************************************/
void display(island *start)
{
island *i = start;
for (;i !=NULL; i = i->next) {
printf("Name: %s \n Open: %s-%s\n\n\n", i->name, i->opens,
i->关闭);
}
}
/*************************************** ******/`
`island *start = NULL;`
`island *i = NULL;`
`island *next = NULL;`
`int j;`
`char name[80];`
`printf("Enter ^Z to finish [in windows]\n");`
`printf("Enter the names of the island you want to create:");`
`for(; (fgets(name, 80, stdin)!= NULL); i = next) {`
`printf("Enter the next name of the island you want to create:");`
`next = create(name);`
` if (start == NULL)`
` start = next;`
` if (i != NULL)`
`i->next = next;`
`}`
`display(start);`
`return 0;`
}
我正在尝试使用 fgets
从 stdin
中读取数据,但是是小块读取。我的问题是我不确定在标准输入结束时 fgets
等于什么。从我的代码来看,它似乎显然不是 NULL
或像 \n
这样的行尾。执行此操作的正确方法是什么?
代码(永远循环,因为 fgets
永远不会等于 NULL
):
int main ()
{
char str1[4];
printf("Enter stuff: ");
while (fgets(str1, sizeof str1, stdin) != NULL &&
fgets(str1, sizeof str1, stdin) != "\n") {
printf("Got here");
}
printf("%s\n",str1);
return 0;
}
改变
while(fgets(str1, sizeof str1, stdin) != NULL && fgets(str1, sizeof str1, stdin) != '\n'){
到
while(fgets(str1, sizeof str1, stdin) != NULL){
或到
while(fgets(str1, sizeof str1, stdin)){
fgets
returns一个char*
。您无法将它与 '\n'
进行比较,后者是 char
。这个函数,遇到EOF
时returnsNULL
。您可以通过按
stdin
上模拟 EOF
- CTRL+Z 在 windows
- CTRL+D 在 linux
你可以这样做:
while (fgets(str1, sizeof str1, stdin) != NULL && str1[0] != '\n')
如果 fgets()
读取换行符,它会将其存储在字符串中,如果遇到 EOF
,则 returns NULL
。
这样你得到输入并测试如果 fgets()
首先遇到 EOF
,然后你测试字符串中的第一个字符 (str1[0]
) 看它是否是换行符。
记住 fgets()
returns a char *
所以你可以使用指针符号 *str1
或数组符号 str1[0]
来测试字符串的第一个字符。
// Rajat Sewal
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct island {
char *name;
char *opens;
char *closes;
struct island *next;
} island;
int main()
{
printf("This is an example of linked lists using a file to read the data needed!\n");
printf("to create the structs dynamically!\n\n\n");
// First we need a function called island that creates the new island struct
/********************************************/
island* create(char *name)
{
island *i = malloc(sizeof(island));
// set the fields on the new struct
i->name = strdup(name); // strdup duplicates the string so a NEW copy is used
i->opens = "09:00";
i->closes = "17:00";
i->next = NULL;
return i;
}
/********************************************/
void display(island *start)
{
island *i = start;
for (;i !=NULL; i = i->next) {
printf("Name: %s \n Open: %s-%s\n\n\n", i->name, i->opens,
i->关闭);
}
}
/*************************************** ******/`
`island *start = NULL;`
`island *i = NULL;`
`island *next = NULL;`
`int j;`
`char name[80];`
`printf("Enter ^Z to finish [in windows]\n");`
`printf("Enter the names of the island you want to create:");`
`for(; (fgets(name, 80, stdin)!= NULL); i = next) {`
`printf("Enter the next name of the island you want to create:");`
`next = create(name);`
` if (start == NULL)`
` start = next;`
` if (i != NULL)`
`i->next = next;`
`}`
`display(start);`
`return 0;`
}