这是一张图片,当我使用fgets。 (它不能正常工作,出奇的慢!)我得到错误,当我使用fgets而不是得到
,这是一个画面,当谈到获得。 (正常工作)
# define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
struct sNode {
struct sNode* tNode;
int G[20];
};
struct tNode {
struct tNode* prev;
struct tNode* next;
int data;
int used;
};
int convert_input(struct sNode** t_ArrayRef, char string[200])
{
int j = 0, i = 0, temp = 0;
int K[20];
while (string[i] != '\0')
{
temp = 0;
while (string[i] != ' ' && string[i] != '\0')
temp = temp * 10 + (string[i++] - '0');
if (string[i] == ' ')
{
i++;
}
(*t_ArrayRef)->G[j++] = temp;
}
return j - 1;
}
int main() {
int i;
char string[200];
char* str[5];
struct sNode* t = (struct sNode*)malloc(sizeof(struct sNode));
str[0] = string;
//fgets(str[0], sizeof(str[0]), stdin); // doesn't works !!!
gets(str[0]); // works !!!
int c = convert_input(&t, str[0]);
int num = t->G[0];
const int a = num;
struct tNode* tNod[6000];
for (i = 0; i<num; i++) {
tNod[i] = (struct tNode*)malloc(sizeof(struct tNode));
}i = 0;
for (i = 1; i<num; i++) {
tNod[i - 1]->data = i;
tNod[i - 1]->used = 0;
if (i != num - 1) {
tNod[i - 1]->next = tNod[i];
}
else {
tNod[i - 1]->next = tNod[i];
tNod[i]->data = i + 1;
tNod[i]->next = tNod[0];
}
}i = 0;
struct tNode* current;
i = 1; int j = 0; int fCount = 0; int zCount = 0;
current = tNod[i - 1];
printf("<");
while (fCount == 0) {
while (current->used == 1) {
current = current->next;
j++;
if (j > num) {
fCount = 1;
break;
}
}
j = 0;
if (i % t->G[1] == 0 && fCount == 0) {
zCount++;
if (zCount != t->G[0]) {
printf("%d, ", current->data, i);
current->used = 1;
}
else {
printf("%d", current->data, i);
current->used = 1;
}
}
i++;
current = current->next;
}
printf(">");
return 0;
}
任何人都可以向我解释为什么我不能得到它的工作使用与fgets?
1)'的sizeof(STR [0])是''的sizeof(字符*)',而不是缓冲区大小。 – BLUEPIXY
2)由于'fgets'的结果包含一个换行符,因此需要排除换行符。例如,在'fgets'之后使用'strcspn'('#include'),如下所示。例如。 'str [0] [strcspn(str [0],“\ n”)] = 0;' –
BLUEPIXY
欢迎来到Stack Overflow。 请注意,在这里说'谢谢'的首选方式是通过 提高投票的好问题和有用的答案(一旦你有足够的声誉这样做),并接受任何 问题最有用的答案,你问(这也给你一个小小的提升,以你的声望 )。 请参阅[关于]页面,以及[如何在此处提问 ?]和 [当有人回答我的 问题时,我该怎么办? ?](http://stackoverflow.com/help/someone-answers) –