通过 fgets() 接受来自用户的多行输入
Accepting multiple lines of input from user via fgets()
我正在尝试从使用 fgets
的用户那里接受多行 'address',但我在离开 while 循环时遇到了 Segmentation fault (core dumped)
。我可以在循环内同时 printf
address
和 part_of_address
变量而没有任何问题,而在循环中它按预期工作。一旦摆脱循环,它就会着火。
// Define a char array called 'name' accepting up to 25 characters.
char name[25];
// Define a char array called 'part_of_address' accepting up to 80 characters.
char part_of_address[80];
// Define a char array called 'address' accepting up to 80 characters.
char address[80];
// Clean the buffer, just to be safe...
int c;
while ((c = getchar()) != '\n' && c != EOF) {};
// Ask for the user to enter a name for the record using fgets and stdin, store
// the result on the 'name' char array.
printf("\nEnter the name of the user (RETURN when done):");
fgets(name, 25, stdin);
// Ask for the user to enter multiple lines for the address of the record, capture
// each line using fgets to 'part_of_address'
printf("\nEnter the address of the user (DOUBLE-RETURN when done):");
while (1)
{
fgets(part_of_address, 80, stdin);
// If the user hit RETURN on a new line, stop capturing.
if (strlen(part_of_address) == 1)
{
// User hit RETURN
break;
}
// Concatinate the line 'part_of_address' to the multi line 'address'
strcat(address, part_of_address);
}
printf("This doesn't print...");
正如 Michael Walz 在评论中指出的那样,您甚至在没有初始化 address
的情况下第一次使用 strcat(address, part_of_address);
。由于它是一个自动数组,它包含未确定的值,并且您正在调用未定义的行为。甚至第一个 strcat
也可能在 address
数组之后覆盖内存。
只需用char address[80] = "";
或char address[80] = {'[=15=]'};
初始化数组
我正在尝试从使用 fgets
的用户那里接受多行 'address',但我在离开 while 循环时遇到了 Segmentation fault (core dumped)
。我可以在循环内同时 printf
address
和 part_of_address
变量而没有任何问题,而在循环中它按预期工作。一旦摆脱循环,它就会着火。
// Define a char array called 'name' accepting up to 25 characters.
char name[25];
// Define a char array called 'part_of_address' accepting up to 80 characters.
char part_of_address[80];
// Define a char array called 'address' accepting up to 80 characters.
char address[80];
// Clean the buffer, just to be safe...
int c;
while ((c = getchar()) != '\n' && c != EOF) {};
// Ask for the user to enter a name for the record using fgets and stdin, store
// the result on the 'name' char array.
printf("\nEnter the name of the user (RETURN when done):");
fgets(name, 25, stdin);
// Ask for the user to enter multiple lines for the address of the record, capture
// each line using fgets to 'part_of_address'
printf("\nEnter the address of the user (DOUBLE-RETURN when done):");
while (1)
{
fgets(part_of_address, 80, stdin);
// If the user hit RETURN on a new line, stop capturing.
if (strlen(part_of_address) == 1)
{
// User hit RETURN
break;
}
// Concatinate the line 'part_of_address' to the multi line 'address'
strcat(address, part_of_address);
}
printf("This doesn't print...");
正如 Michael Walz 在评论中指出的那样,您甚至在没有初始化 address
的情况下第一次使用 strcat(address, part_of_address);
。由于它是一个自动数组,它包含未确定的值,并且您正在调用未定义的行为。甚至第一个 strcat
也可能在 address
数组之后覆盖内存。
只需用char address[80] = "";
或char address[80] = {'[=15=]'};