Q:

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

belongs to collection: File handling in C

0

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

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 binary file
    fp = fopen("aticleworld.dat", "wb");
    if(fp == NULL)
    {
        printf("Error in creating the file\n");
        exit(1);
    }
    //Get the length of message
    length = strlen(message);
    //write the message len in file
    fwrite(&length, sizeof(int), 1, fp);
    //write message in file
    fwrite(message, sizeof(char),length, fp);
    fclose(fp);
    return 0;
}

Output:

In the below image, you can see the fwrite function write the 22 (Message length)  as it as in binary format, so 22 is ASCII value of SYN and it is showing in the file.

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

total answers (1)

C Program to write a variable in a file using the ... >>
<< C Program to write the message and the length usin...