Q:

C Program to write the message and the length using the fprintf

belongs to collection: File handling in C

0

C Program to write the message and the length using the fprintf

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
#include <string.h>
int main()
{
    //file pointer
    FILE *fp = NULL;
    //Message
    char * message = "I love Aticleworld.com";
    //Variable for message length
    int length = 0;
    //create and open the texr file
    fp = fopen("aticleworld.txt", "w");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
    //Get the length of message
    length = strlen(message);
    //write the length in file
    fprintf(fp,"%d",length);
    //write the message in file
    fprintf(fp,"%s",message);
    //Close the file
    fclose(fp);
    return 0;
}

Output:

In the below image, you can see the fprintf write the 22 (message length) in string format, so it is showing as it as 22.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Program to write the message and the length usin... >>
<< How to use fwrite in c...