1

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.

#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
  • 2
    It would be useful if you showed what you used for example command line arguments and what the contents of the output file is. Also you shouldn't return with no value, you should return 1 to indicate an error. And make sure you #include <stdlib.h> to use atoi() and atof(). Commented Mar 20, 2016 at 1:56

1 Answer 1

2

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

Also note that

  • using return; (return statement without return value) isn't good in non-void function.
  • you should check whether fopen() was successful.

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;
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

Best answer I've seen in ages! Thanks for such a clear, informative answer and thank you for correcting my code in other areas as a bonus! :)
That unguarded strcpy there would be a firing offense in most companies.
@KerrekSB I am a student who is new to learning C, not a professional C programmer. What does it mean to "guard" a strcpy?
@HJGBAUM: turn on all compiler warnings: gcc -Wall -Wextra -Werror, or clang -Weverything -Werror... These prevent lot of silly mistakes such as those in the posted code.
@HJGBAUM If argv[1] == "123456789012345678901234567890", what do you think will happen with char name[20]; strcpy(name, argv[1]);?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.