fgets输出奇怪的垃圾字符

时间:2015-02-26 20:39:12

标签: c arrays string loops fgets

我正在尝试打印fgets从键盘输入中获取的字符串。但是当我运行该程序时,我得到了一个无穷无尽的奇怪字符循环。为什么呢?

这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define SIZE 6

void stringF();
void revString();
void stringLength();
void verticalString();
void triString();

int main() {
  char string[SIZE];
  stringF(&string[0]);
  system("pause");
  return 0;
}

void stringF(char* str) {
  fgets(str, SIZE, stdin);
  while (str != '\0') {
    putchar(str);
    str++;
  }
}

1 个答案:

答案 0 :(得分:3)

重写循环,如

if ( fgets(str, SIZE, stdin) != NULL )
{

   while ( *str != '\0'){
        putchar(*str);
        str++;
    }
}
相关问题