gpt4 book ai didi

C fgets strtok 和 atoi 读取 C 中的一行

转载 作者:太空宇宙 更新时间:2023-11-04 08:36:03 32 4
gpt4 key购买 nike

#include <stdio.h>
#include <string.h>

int main(void) {
char string[10000],*token;
int garden[100],i=0;
fgets(string,10000,stdin);
token = strtok(string," ");
while(strcmp(token,"\n") != 0){
garden[i] = atoi(token);
i++;
token = strtok(NULL," ");
}
return 0;
}

这段代码有什么问题?为什么我不能从一行中读取空格分隔的整数?

最佳答案

改变

 while(strcmp(token,"\n") != 0){

while(token != NULL){

关于C fgets strtok 和 atoi 读取 C 中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26191011/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com