C 将字符串动态转换为浮点

C 将字符串动态转换为浮点,c,C,我在下面的代码中遇到了一些问题 代码的主要思想是逐行读取并将字符字符串转换为浮点,然后将浮点保存在名为nfloat的数组中 输入是一个.txt,包含以下内容:n=字符串数,在本例中为n=3 第一个数字,3是我们可以在图像中看到的向量数,但该数字不是静态的,输入可以是5或7,等等,而不是3 到目前为止,我已经开始做以下工作(仅针对1个向量情况),但我认为代码存在一些内存错误: int main(){ int n; //number of string, comes in the input

我在下面的代码中遇到了一些问题

代码的主要思想是逐行读取并将字符字符串转换为浮点,然后将浮点保存在名为
nfloat
的数组中

输入是一个
.txt
,包含以下内容:n=字符串数,在本例中为n=3

第一个数字,
3
是我们可以在图像中看到的向量数,但该数字不是静态的,输入可以是
5
7
,等等,而不是
3

到目前为止,我已经开始做以下工作(仅针对1个向量情况),但我认为代码存在一些内存错误:

int main(){
    int n; //number of string, comes in the input
    scanf("%d\n", &n);
    char *line = NULL;
    size_t len = 0;
    ssize_t read;
    read = getline(&line,&len,stdin); //here the program assigns memory for the 1st string
    int numsvector = NumsVector(line, read);//calculate the amount of numbers in the strng
    float nfloat[numsvector];
    int i;
    for (i = 0; i < numsvector; ++i)
    {
        if(numsvector == 1){
            sscanf(line, "[%f]", &nfloat[i]);
        }
        else if(numsvector == 2){
            if(i == 0) {
                sscanf(line, "[%f,", &nfloat[i]);
                printf("%f ", nfloat[i]);
            }
            else if(i == (numsvector-1)){
                sscanf((line+1), "%f]", &nfloat[i]);
                printf("%f\n", nfloat[i]);
            }
        }
    else {   //Here is where I think the problems are
        if(i == 0) {
            sscanf(line, "[%f,", &nfloat[i]);
            printf("%f\n", nfloat[i]);

        }
        else if(i == (numsvector-1)) {
            sscanf((line+1+(4*i)), "%f]", &nfloat[i]);
            printf("%f\n", nfloat[i]);
        }
        else {
            sscanf((line+1+(4*i)), "%f,", &nfloat[i]);
            printf("%f\n", nfloat[i]);
        }
    }
}

请有人帮我了解问题出在哪里吗?

好的-如果您用这个替换当前的for循环,您的nfloat数组中应该有正确的数字

/* Replaces the end ] with a , */
line[strlen(line) - 1] = ',';

/* creates a new pointer, pointing after the first [ in the original string */
char *p = line + 1;
do
{
    /* grabs up to the next comma as a float */
    sscanf(p, "%f,", &nfloat[i]);
    /* prints the float it's just grabbed to 2 dp */
    printf("%.2f\n",nfloat[i]);
    /* moves pointer forward to next comma */
    while (*(p++) != ',');
}
while (++i < numsvector); /* stops when you've got the expected number */
/*将结尾]替换为*/
行[strlen(line)-1]=',';
/*创建一个新指针,指向原始字符串中的第一个[后]*/
char*p=line+1;
做
{
/*抓取下一个逗号作为浮点数*/
sscanf(p,,%f,,&nfloat[i]);
/*打印浮子它刚抓到2 dp*/
printf(“%.2f\n”,nfloat[i]);
/*将指针向前移动到下一个逗号*/
而(*(p++)!=',');
}
而(++i
看不到任何证明计算正确的东西
(第+1行+(4*i))
。它假设您的浮点数有三个字符长,但即使在您提供的数据中也不是这样。我认为这种方法是错误的,需要某种标记化,甚至可能需要使用strtok。是的,我也这么认为,但我只能使用sscanf语句:(我在您的代码中看不到任何与相关的内容。请删除标记或选择其中一个。getline函数如何,您可以共享该函数的代码吗?如果没有其他功能,它将允许某人运行该程序,并查看他们是否有与您相同的问题。为什么使用
linea[n]==44
而不是
linea[n]==','
?棒极了!它可以工作:D!但它会打印一些垃圾,例如在我们可以看到的输入中:[9.3,1.2,87.9],但在执行一些打印时我们可以看到:[9.300000,1.200000,87.900002]@Gerard通常,你会通过投票/接受他们的回答来感谢stackoverflow中的某个人。我想你看到的是浮点精度错误。除了打印时只使用2D.p.之外,你做不了什么,因为数据似乎是那样的。我已经更新了上面的答案,将2D.p.printf语句包括在内。
int NumsVector(char *linea, ssize_t size){
        int numsvector = 1; //minimum value = 1
        int n;
        for(n = 2; n<= size; n++){
            if (linea[n] != '[' && linea[n] != ']'){
                if(linea[n] == 44){
                    numsvector = numsvector + 1;
                }
            }
        }
        return numsvector;
}
/* Replaces the end ] with a , */
line[strlen(line) - 1] = ',';

/* creates a new pointer, pointing after the first [ in the original string */
char *p = line + 1;
do
{
    /* grabs up to the next comma as a float */
    sscanf(p, "%f,", &nfloat[i]);
    /* prints the float it's just grabbed to 2 dp */
    printf("%.2f\n",nfloat[i]);
    /* moves pointer forward to next comma */
    while (*(p++) != ',');
}
while (++i < numsvector); /* stops when you've got the expected number */