cOutput language typed strings to a text file

Asked 2 years ago, Updated 2 years ago, 85 views

#include <stdio.h>

int main(void)
{
    char sts[50];
    int cnt;
    FILE* foc = NULL;
    errno_t err;

    err = fopen_s(&foc, "save.txt", "wt");

    if (err == 0)
    {
        printf("save.txt file created\n");
        printf("1. Enter string : ");
        gets_s(sts, sizeof(sts));

        printf("2. What I wrote in the file: %s\n", sts);

        for (cnt = 0; sts[cnt] != NULL; cnt++);
        fputc(sts[cnt], foc);

        fclose(foc);
        printf("save.txt file close successfully\n");

    }
    else
        printf("Save.txt file creation failed\n";

    return 0;
}

I ran it and opened the save.txt file, but nothing was recorded. For your information, I wrote down the example questions in the book and checked several times to see if they were correct, so is there any other reason?

c file-io

2022-09-20 11:39

1 Answers

The problem is due to ; at the end of the for loop.

Clear ; as shown below.

for (cnt = 0; sts[cnt] != NULL; cnt++)
            fputc(sts[cnt], foc);


2022-09-20 11:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.