I made a notepad, but there's nothing written on it

Asked 2 years ago, Updated 2 years ago, 31 views

Hello, I'm a beginner in C language.

#include <stdio.h>

int main(void)
{
    int i = 0;
    char name[1001];
    printf ("What is your name?" : ");
    gets(name);

    FILE *file = NULL;
    file = fopen("your_name.txt", "w");

    if (file == NULL)
    {
        printf("Failed to open file.");
    }
    else
    {
        printf ("The file was successfully opened.");
    }

    while (name[i] != '\0')
    {
        fputs(name[i], file);
        i++;
    }

    fclose(file);
}

If you look at this code and think about it, it should have the name you entered in your_name, but it's actually made, but there's nothing inside. There is no error, but the warning message is

[Warning] passing argument 1 of 'fputs' makes pointer from integer without a cast

That's what he said. Did I do anything wrong?

c

2022-09-22 19:08

1 Answers

First, look at the prototype of the fputs function.

int fputs(const char* str, FILE* stream);

The fputs function is a function that writes a string to stream.

It's not a character, it's a string (!).

In other words, you need to insert a pointer to char.

Please modify it as below and try it.

//while (name[i] != '\0')
//{
    fputs(name, file);
//    //    i++;
//}


2022-09-22 19:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.