sscanf循环仅多次读取第一个输入

sscanf循环仅多次读取第一个输入,c,string,while-loop,segmentation-fault,scanf,C,String,While Loop,Segmentation Fault,Scanf,我使用sscanf分割从输入中提取的一个字符串,并将每个令牌存储在一个结构中。问题是,sscanf只读取字符串的第一个字,而不前进到下一个字,反复打印相同的标记。这是密码 #include <stdio.h> #include <stdlib.h> #include <string.h> #define dim 30 typedef struct string { char* token[dim]; }string; int main() {

我使用sscanf分割从输入中提取的一个字符串,并将每个令牌存储在一个结构中。问题是,sscanf只读取字符串的第一个字,而不前进到下一个字,反复打印相同的标记。这是密码

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

#define dim 30

typedef struct string { 
  char* token[dim];
}string;

int main() {
  string* New = (string *)malloc(dim*sizeof(string));
  char* s;
  char buffer[dim];
  int i = 0, r = 0, j = 0;
  s = (char*)malloc(sizeof(char*));
  printf("\nString to read:\n");
  fgets(s, dim, stdin);
  printf("\nThe string is: %s", s); 
  while(sscanf(s, " %s ", buffer) != EOF) {
    New->token[i] = malloc(dim*sizeof(char));
    strcpy(New->token[i], buffer);
    printf("\nAdded: %s", New->token[i]);
    ++i;
  }
}
#包括
#包括
#包括
#定义尺寸30
typedef结构字符串{
字符*标记[dim];
}弦;
int main(){
string*New=(string*)malloc(dim*sizeof(string));
char*s;
字符缓冲区[dim];
int i=0,r=0,j=0;
s=(char*)malloc(sizeof(char*));
printf(“\n字符串读取:\n”);
FGET(s、dim、stdin);
printf(“\n字符串为:%s”,s);
while(sscanf(s),%s,buffer)!=EOF){
新建->令牌[i]=malloc(dim*sizeof(char));
strcpy(新建->令牌[i],缓冲区);
printf(“\n添加:%s”,新建->令牌[i]);
++一,;
}
}

例如,如果我将“this is a string”作为输入,sscanf只会多次获取单词“this”,而不会继续下一个单词。

您需要增加源读取的指针,以便它不会一次又一次地从同一点读取

此外,您为
s
动态分配的内存没有任何意义。无论如何都太少了。通过在代码后面调用
fgets()
,我可以看出您的意思是说
s=malloc(dim*sizeof(char)),所以我继续并修复了它

例如:

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

#define dim 30

typedef struct string {
  char* token[dim];
} string;

int main() {
  string* New = malloc(dim*sizeof(string));
  char* s;
  char buffer[dim];
  int i = 0;
  s = malloc(dim * sizeof(char));
  fgets(s, dim, stdin);
  printf("The string is: %s\n", s); 
  char* ptr = s;
  int offset;
  while (sscanf(ptr, "%s%n", buffer, &offset) == 1) {
    ptr += offset;
    New->token[i] = malloc(strlen(buffer) + 1);
    strcpy(New->token[i], buffer);
    printf("Added: %s\n", New->token[i]);
    ++i;
  }

  // do work

  for(int j = 0; j < i; ++j)
    free(New->token[i]);
  free(New);
  free(s);

  return 0;
}
PS:我不确定你心目中的结构模式,也许你需要花一两分钟,反复思考;我的意思是你的设计方法是否有意义

PPS:与您的问题无关:不

编辑:正如@chux所说,
sscanf()
“%s%n”
中的
没有任何作用。我将其更改为
“%s%n”


此外,为了准确地保留所需的内存(这是处理动态内存分配时要做的事情),
New->token[i]=malloc(dim*sizeof(char))更改为
New->token[i]=malloc(strlen(buffer)+1)

您需要增加源读取的指针,以便它不会一次又一次地从同一点读取

此外,您为
s
动态分配的内存没有任何意义。无论如何都太少了。通过在代码后面调用
fgets()
,我可以看出您的意思是说
s=malloc(dim*sizeof(char)),所以我继续并修复了它

例如:

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

#define dim 30

typedef struct string {
  char* token[dim];
} string;

int main() {
  string* New = malloc(dim*sizeof(string));
  char* s;
  char buffer[dim];
  int i = 0;
  s = malloc(dim * sizeof(char));
  fgets(s, dim, stdin);
  printf("The string is: %s\n", s); 
  char* ptr = s;
  int offset;
  while (sscanf(ptr, "%s%n", buffer, &offset) == 1) {
    ptr += offset;
    New->token[i] = malloc(strlen(buffer) + 1);
    strcpy(New->token[i], buffer);
    printf("Added: %s\n", New->token[i]);
    ++i;
  }

  // do work

  for(int j = 0; j < i; ++j)
    free(New->token[i]);
  free(New);
  free(s);

  return 0;
}
PS:我不确定你心目中的结构模式,也许你需要花一两分钟,反复思考;我的意思是你的设计方法是否有意义

PPS:与您的问题无关:不

编辑:正如@chux所说,
sscanf()
“%s%n”
中的
没有任何作用。我将其更改为
“%s%n”


此外,为了准确地保留所需的内存(这是处理动态内存分配时要做的事情),
New->token[i]=malloc(dim*sizeof(char))更改为
New->token[i]=malloc(strlen(buffer)+1)

您的输入是什么?不错的MCVE,但需要一点缩进。:)
s
指向一个大小为
sizeof(char*)
的内存,该内存太小,而且没有意义。另外,您对
New
的分配也有问题,因为您只使用了第一个元素。@gsamaras我以一个示例进行了编辑。谢谢。你对wako有什么意见?不错的MCVE,但需要一点缩进。:)
s
指向一个大小为
sizeof(char*)
的内存,该内存太小,而且没有意义。另外,您对
New
的分配也有问题,因为您只使用了第一个元素。@gsamaras我以一个示例进行了编辑。谢谢。注意:
“%s%n”
中的
没有任何作用。可以改为使用
“%s%n”
New->token[i]=malloc(dim*sizeof(char))New->token[i]=malloc(strlen(buffer)+1)时,code>的大小效率更高是@chux,好提示!在发布这个答案时,有太多的事情要做,所以我错过了这些问题,答案得到了改进,谢谢投票!注意:
“%s%n”
中的
没有任何作用。可以改为使用
“%s%n”
New->token[i]=malloc(dim*sizeof(char))New->token[i]=malloc(strlen(buffer)+1)时,code>的大小效率更高是@chux,好提示!在发布这个答案时,有太多的事情要做,所以我错过了这些问题,答案得到了改进,谢谢投票!