我应该如何使用 fgetc 忽略来自 stdin 的换行符?
How should I ignore newline from stdin using fgetc?
我在使用 fgetc()
函数填充固定大小的二维数组时遇到问题。
我的程序应该只读 '-'
、'+'
或 '!'
,一行中输入的字符必须等于列的大小。
这是我的代码中有问题的部分:
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
c = fgetc( stdin );
if( (c != '-') && (c != '+') && (c != '!') ) {
printf( "Incorrect value.\n" );
return 1;
}
array[i][j] = c;
}
if(array[i+1][0] != '\n') {
printf( "Incorrect value (size does not equal to columns).\n" );
return 1;
}
}/*---end of for---*/
以下是我的理解:
fgetc()
也会扫描换行符 ('\n') 并将其放入下一行 - 这意味着 array[1][0]
应该是 '\n'
。如果用户输入的字符多于 cols 设置的字符数,它将是除换行符之外的其他字符,程序将以错误结束。然而,这不起作用。
是否有更好的方法来忽略来自标准输入的换行符并检查用户是否输入了比之前指定的更多的字符?
我不是特别容忍 fgetc() 的使用,但如果您坚持使用它,我认为下面是您想要执行的操作的工作版本。
关键是,在使用 fgetc() 读取一个字符后,您读取另一个字符以吞下换行符,然后继续...
#include <stdio.h>
int main() {
int rows = 3, cols = 3;
int i, j;
char array[rows][cols];
char c;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
c = fgetc(stdin);
if( (c != '-') && (c != '+') && (c != '!') ) {
printf( "Incorrect value.\n" );
return 1;
}
else {
array[i][j] = c;
// swallow the newline
fgetc(stdin);
}
}
}
// print all
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%c ", array[i][j]);
}
printf("\n");
}
}
编辑
按照下面的建议,如果你想在一行中输入所有字符,然后按换行符,这样:
val00 val01 val02 [newline]
val10 val11 val12 [newline]
val20 val21 val22 [newline]
那么下面的方法就可以了。 [注意不要有尾随空格,因为它可能会破坏预期的功能]:
#include <stdio.h>
int main() {
int rows = 3, cols = 3;
int i, j;
char array[rows][cols];
char c;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
c = fgetc(stdin);
if( (c != '-') && (c != '+') && (c != '!') ) {
printf( "Incorrect value.\n" );
return 1;
}
else if (c == '\n') {
fgetc(stdin);
continue;
}
array[i][j] = c;
fgetc(stdin); // swallow space
}
}
// print all
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%c ", array[i][j]);
}
printf("\n");
}
}
根据 '\n'
测试值并使用 int
。
不要将 fgetc()
中的 return 值保存到 char
数组中,稍后再进行测试。这失去了区分 EOF
和其他 char
的能力。
当需要 '\n'
(或 EOF)时,这很好。
推荐使用正逻辑测试,更容易理解
for (i = 0; i < rows; i++) {
int c;
for (j = 0; j < cols; j++) {
c = fgetc( stdin );
if (c == '-' || c == '+' || c == '!') {
array[i][j] = c;
} else {
puts("Incorrect value.");
return 1;
}
}
c = fgetc( stdin ); // Read and consume the end-of-line
if (!(c == '\n' || c == EOF)) {
printf( "Incorrect value (size does not equal to columns).\n" );
return 1;
}
}
我在使用 fgetc()
函数填充固定大小的二维数组时遇到问题。
我的程序应该只读 '-'
、'+'
或 '!'
,一行中输入的字符必须等于列的大小。
这是我的代码中有问题的部分:
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
c = fgetc( stdin );
if( (c != '-') && (c != '+') && (c != '!') ) {
printf( "Incorrect value.\n" );
return 1;
}
array[i][j] = c;
}
if(array[i+1][0] != '\n') {
printf( "Incorrect value (size does not equal to columns).\n" );
return 1;
}
}/*---end of for---*/
以下是我的理解:
fgetc()
也会扫描换行符 ('\n') 并将其放入下一行 - 这意味着 array[1][0]
应该是 '\n'
。如果用户输入的字符多于 cols 设置的字符数,它将是除换行符之外的其他字符,程序将以错误结束。然而,这不起作用。
是否有更好的方法来忽略来自标准输入的换行符并检查用户是否输入了比之前指定的更多的字符?
我不是特别容忍 fgetc() 的使用,但如果您坚持使用它,我认为下面是您想要执行的操作的工作版本。
关键是,在使用 fgetc() 读取一个字符后,您读取另一个字符以吞下换行符,然后继续...
#include <stdio.h>
int main() {
int rows = 3, cols = 3;
int i, j;
char array[rows][cols];
char c;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
c = fgetc(stdin);
if( (c != '-') && (c != '+') && (c != '!') ) {
printf( "Incorrect value.\n" );
return 1;
}
else {
array[i][j] = c;
// swallow the newline
fgetc(stdin);
}
}
}
// print all
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%c ", array[i][j]);
}
printf("\n");
}
}
编辑
按照下面的建议,如果你想在一行中输入所有字符,然后按换行符,这样:
val00 val01 val02 [newline] val10 val11 val12 [newline] val20 val21 val22 [newline]
那么下面的方法就可以了。 [注意不要有尾随空格,因为它可能会破坏预期的功能]:
#include <stdio.h>
int main() {
int rows = 3, cols = 3;
int i, j;
char array[rows][cols];
char c;
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
c = fgetc(stdin);
if( (c != '-') && (c != '+') && (c != '!') ) {
printf( "Incorrect value.\n" );
return 1;
}
else if (c == '\n') {
fgetc(stdin);
continue;
}
array[i][j] = c;
fgetc(stdin); // swallow space
}
}
// print all
for(i = 0; i < rows; i++) {
for(j = 0; j < cols; j++) {
printf("%c ", array[i][j]);
}
printf("\n");
}
}
根据 '\n'
测试值并使用 int
。
不要将 fgetc()
中的 return 值保存到 char
数组中,稍后再进行测试。这失去了区分 EOF
和其他 char
的能力。
当需要 '\n'
(或 EOF)时,这很好。
推荐使用正逻辑测试,更容易理解
for (i = 0; i < rows; i++) {
int c;
for (j = 0; j < cols; j++) {
c = fgetc( stdin );
if (c == '-' || c == '+' || c == '!') {
array[i][j] = c;
} else {
puts("Incorrect value.");
return 1;
}
}
c = fgetc( stdin ); // Read and consume the end-of-line
if (!(c == '\n' || c == EOF)) {
printf( "Incorrect value (size does not equal to columns).\n" );
return 1;
}
}