在C问题中将字符串转换为float?

时间:2021-09-04 19:49:31

So I have a C program where you type a name, age and height as command line prompts and these arguments are written to a text file, however, with the height (which is a float), there's a problem. It writes down a value that is really high and isn't what you type in. I feel like there's a problem with the memory or something similar.

所以我有一个C程序,你输入一个名称,年龄和高度作为命令行提示,这些参数写入文本文件,但是,高度(这是一个浮点数),有一个问题。它写下了一个非常高的值,而不是你输入的内容。我觉得内存或类似内容存在问题。

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

int main(int argc, char *argv[]) {
    if (argc != 4) {
        printf("Please enter four parameters");
        return;
    } else {
        char name[20];
        strcpy(name, argv[1]);
        int age = atoi(argv[2]);
        double height = atof(argv[3]);
        FILE *fp;

        fp = fopen("name.txt", "w+");
        fprintf(fp, "%s\n%d\n%.2f", name, age, height);
        fclose(fp);

        printf("File written!");

        return 0;
    }
}

So what have I done wrong with the float height?

那么浮标高度我做错了什么?

1 个解决方案

#1


2  

You have to include proper header (stdlib.h) or declare the functions before using them to use atoi() and atof().

您必须包含正确的标头(stdlib.h)或在使用它们之前声明函数以使用atoi()和atof()。

Also note that

另请注意

  • using return; (return statement without return value) isn't good in non-void function.
  • 使用回报; (没有返回值的return语句)在非void函数中不好。
  • you should check whether fopen() was successful.
  • 你应该检查fopen()是否成功。

Try this:

尝试这个:

#include <stdio.h>
#include <stdlib.h> /* add this */
#include <string.h>

int main(int argc, char *argv[]) {
    if(argc != 4) {
        printf("Please enter four parameters");
        return 1; /* add a return value */
    }
    else {
        char name[20];
        strcpy(name, argv[1]);
        int age = atoi(argv[2]);
        double height = atof(argv[3]);
        FILE *fp;

        fp = fopen("name.txt", "w+");
        if(fp == NULL) { /* add error check */
            perror("fopen");
            return 1;
        }
        fprintf(fp, "%s\n%d\n%.2f", name, age, height);
        fclose(fp);

        printf("File written!");

        return 0;
    }
}

#1


2  

You have to include proper header (stdlib.h) or declare the functions before using them to use atoi() and atof().

您必须包含正确的标头(stdlib.h)或在使用它们之前声明函数以使用atoi()和atof()。

Also note that

另请注意

  • using return; (return statement without return value) isn't good in non-void function.
  • 使用回报; (没有返回值的return语句)在非void函数中不好。
  • you should check whether fopen() was successful.
  • 你应该检查fopen()是否成功。

Try this:

尝试这个:

#include <stdio.h>
#include <stdlib.h> /* add this */
#include <string.h>

int main(int argc, char *argv[]) {
    if(argc != 4) {
        printf("Please enter four parameters");
        return 1; /* add a return value */
    }
    else {
        char name[20];
        strcpy(name, argv[1]);
        int age = atoi(argv[2]);
        double height = atof(argv[3]);
        FILE *fp;

        fp = fopen("name.txt", "w+");
        if(fp == NULL) { /* add error check */
            perror("fopen");
            return 1;
        }
        fprintf(fp, "%s\n%d\n%.2f", name, age, height);
        fclose(fp);

        printf("File written!");

        return 0;
    }
}