在C语言中将字符数组的一部分更改为整数数组
我正在查看一个包含单词和数字的数据文件,我需要从每行中取出数字并将它们存储在一个数组中在C语言中将字符数组的一部分更改为整数数组,c,arrays,string,C,Arrays,String,我正在查看一个包含单词和数字的数据文件,我需要从每行中取出数字并将它们存储在一个数组中 Cheryl 2 1 0 1 2 0 Neal 0 0 2 0 2 0 Henry 0 2 2 0 2 0 Lisa 0 0 0 0 2 1 这就是文件的格式。我首先将每一行输入一个数组,participants[],然后我需要从每一行中提取数字,分别放入一个单独的二维数组中。i、 e.第一行将翻译为: responses[0][0] = 2 responses[0][1] = 1 respon
Cheryl 2 1 0 1 2 0
Neal 0 0 2 0 2 0
Henry 0 2 2 0 2 0
Lisa 0 0 0 0 2 1
这就是文件的格式。我首先将每一行输入一个数组,participants[]
,然后我需要从每一行中提取数字,分别放入一个单独的二维数组中。i、 e.第一行将翻译为:
responses[0][0] = 2
responses[0][1] = 1
responses[0][2] = 0
responses[0][3] = 1
responses[0][4] = 2
responses[0][5] = 0
此时,我用char*pointer=strstrstr(“,participants[])检测第一个空格代码>,我能用strcpy(临时,指针+1)找到数字的开头代码>
从那时起,我遇到了一些问题,因为我很难将数字字符串(仍然是char
string)转换为单个整数值,以存储在我的responses[][]
数组中
谢谢 如果这就是你的确切格式,那么也许fscanf
/sscanf
可以为你做这项工作?即
#include <stdio.h>
int main(void)
{
int nums[6];
char line[] = "Cheryl 2 1 0 1 2 0";
sscanf(line, "%*s %d %d %d %d %d %d", &nums[0], &nums[1], &nums[2], &nums[3], &nums[4], &nums[5]);
printf("%d %d %d %d %d %d\n", nums[0], nums[1], nums[2], nums[3], nums[4], nums[5]);
return 0;
}
然后检查返回值以确保读取的数字量正确
编辑:对于任意金额:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strchr_ignore_start(const char *str, int c)
{
if (c == '\0') return strchr(str, c); /* just to be pedantic */
/* ignore c from the start of the string until there's a different char */
while (*str == c) str++;
return strchr(str, c);
}
size_t extractNums(char *line, unsigned *nums, size_t num_count)
{
size_t i, count;
char *endptr = line;
for (i = 0, count = 0; i < num_count; i++, count++) {
nums[i] = strtoul(line, &endptr, 10);
if (line == endptr) /* no digits, strtol failed */
break;
line = endptr;
}
return count;
}
int main(void)
{
unsigned i, nums[9];
char line[] = "Cheryl 2 1 0 1 2 0 0 1 2";
char *without_name;
/* point to the first space AFTER the name (we don't want ones before) */
without_name = strchr_ignore_start(line, ' ');
printf("%u numbers were successfully read\n",
(unsigned)extractNums(without_name, nums, 9));
for (i = 0; i < 9; i++)
printf("%d ", nums[i]);
putchar('\n');
return EXIT_SUCCESS;
}
#包括
#包括
#包括
字符*strchr\u忽略\u开始(常量字符*str,int c)
{
if(c=='\0')返回strchr(str,c);/*只是为了学究*/
/*忽略字符串开头的c,直到出现不同的字符*/
而(*str==c)str++;
返回strchr(str,c);
}
大小提取nums(字符*行,无符号*nums,大小提取num\u计数)
{
尺寸i,计数;
char*endptr=行;
对于(i=0,count=0;i
如果这就是你的确切格式,那么也许fscanf
/sscanf
可以为你做这项工作?即
#include <stdio.h>
int main(void)
{
int nums[6];
char line[] = "Cheryl 2 1 0 1 2 0";
sscanf(line, "%*s %d %d %d %d %d %d", &nums[0], &nums[1], &nums[2], &nums[3], &nums[4], &nums[5]);
printf("%d %d %d %d %d %d\n", nums[0], nums[1], nums[2], nums[3], nums[4], nums[5]);
return 0;
}
然后检查返回值以确保读取的数字量正确
编辑:对于任意金额:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *strchr_ignore_start(const char *str, int c)
{
if (c == '\0') return strchr(str, c); /* just to be pedantic */
/* ignore c from the start of the string until there's a different char */
while (*str == c) str++;
return strchr(str, c);
}
size_t extractNums(char *line, unsigned *nums, size_t num_count)
{
size_t i, count;
char *endptr = line;
for (i = 0, count = 0; i < num_count; i++, count++) {
nums[i] = strtoul(line, &endptr, 10);
if (line == endptr) /* no digits, strtol failed */
break;
line = endptr;
}
return count;
}
int main(void)
{
unsigned i, nums[9];
char line[] = "Cheryl 2 1 0 1 2 0 0 1 2";
char *without_name;
/* point to the first space AFTER the name (we don't want ones before) */
without_name = strchr_ignore_start(line, ' ');
printf("%u numbers were successfully read\n",
(unsigned)extractNums(without_name, nums, 9));
for (i = 0; i < 9; i++)
printf("%d ", nums[i]);
putchar('\n');
return EXIT_SUCCESS;
}
#包括
#包括
#包括
字符*strchr\u忽略\u开始(常量字符*str,int c)
{
if(c=='\0')返回strchr(str,c);/*只是为了学究*/
/*忽略字符串开头的c,直到出现不同的字符*/
而(*str==c)str++;
返回strchr(str,c);
}
大小提取nums(字符*行,无符号*nums,大小提取num\u计数)
{
尺寸i,计数;
char*endptr=行;
对于(i=0,count=0;i
strtok()
似乎是这里显而易见的解决方案:
char buf[1024];
int y;
for(y=0;fgets(buf, sizeof buf, file); y++){
strtok(buf, " "); //burn name;
char *str;
int x = 0;
for(str=strtok(NULL, " "); str; str=strtok(NULL, " ")){
responses[y][x++] = atoi(str);
}
}
strtok()
似乎是这里显而易见的解决方案:
char buf[1024];
int y;
for(y=0;fgets(buf, sizeof buf, file); y++){
strtok(buf, " "); //burn name;
char *str;
int x = 0;
for(str=strtok(NULL, " "); str; str=strtok(NULL, " ")){
responses[y][x++] = atoi(str);
}
}
尝试strtoul()
。如果你玩得好的话,这甚至可以为你解决标记化问题。1)、2)也许你可以使用可选的endptr
参数来计算下一个数字的起始位置。试试strtoul()
。如果你玩得好的话,这甚至可以为你解决标记化问题。1)、2)也许你可以使用可选的endptr
参数来计算下一个数字的起始位置。我需要该方法是可扩展的,这样如果有9个选项而不是6个,它就可以存储所有9个选项。是否有办法修改此项以满足该标准?然而,你给我的正是我所需要的@iKiar你将如何提取这些数字?在尝试提取数字之前,是否已将每行读入字符串?它们是有符号的还是无符号的?是的,字符串类似于charline[]=“Cheryl 2 1 0 1 2 0 1 2”代码>与字符行[]=“Cheryl 2 1 0 1 2 0”相对代码>。。当然,这也可以是另一种方式,它有一个字符串,比如charline[]=“Cheryl 1 2 0”代码>并且唯一可能的值是0,1,2。因此,虽然它们不是无符号变量类型,但它们不是有符号的。@iKiar我又编辑了一次,我不喜欢可能出现的空白问题。我需要该方法具有可扩展性,因此如果有9个选项而不是6个,它将存储所有9个选项。是否有办法修改此项以满足该标准?然而,你给我的正是我所需要的@iKiar你将如何提取这些数字?在尝试提取数字之前,是否已将每行读入字符串?它们是有符号的还是无符号的?是的,字符串类似于charline[]=“Cheryl 2 1 0 1 2 0 1 2”代码>与字符行[]=“Cheryl 2 1 0 1 2 0”相对代码>。。当然,这也可以是另一种方式,它有一个字符串,比如charline[]=“Cheryl 1 2 0”代码>并且唯一可能的值是0,1,2。因此,虽然它们不是无符号变量类型,但它们不是有符号的。@iKiar我又编辑了一次,我不喜欢潜在的空白问题。