Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6745191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:10:30+00:00 2026-05-26T12:10:30+00:00

see my code is #include <stdio.h> #include<stdlib.h> #include<sys/stat.h> int main(int argc, char**argv) { unsigned

  • 0

see my code is

#include <stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
int main(int argc, char**argv) {

    unsigned char *message = NULL;

    struct stat stp = { 0 };
    stat("huffman.txt", &stp);
    /*determine the size of data which is in file*/
    int filesize = stp.st_size;
    printf("\nFile size id %d\n", filesize);

    FILE* original_fileptr = fopen("huffman.txt", "r");
    message =  malloc(filesize);
    fread(message, 1, filesize, original_fileptr);
    printf("\n\tEntered Message for Encode is %s= and length %d", message,strlen(message));

    return 0;
}

here huffman.txt has size 20 bytes and following character are there

ά­SUä5Ñ®qøá”F„œ

output of this code is

File size id 20

        Entered Message for Encode is ά­SUä5Ñ®qøá"F„œ= and length 21

now question is if size is 20 then why length is 21 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:10:30+00:00Added an answer on May 26, 2026 at 12:10 pm

    Because C doesn’t have native strings, only arrays of characters, and there is a hidden, ubiquitous assumption that the last array member is a zero.

    Since you violate that assumption by reading only 20 bytes into an array of 20 elements, with no regard to whether the final byte is zero, and then using string features like %s and strlen, you get essentially undefined behavior.

    Getting an answer of 21 is pure luck; anything (far worse) could have happened.

    Correct code could be something like this (assuming the file is a text file):

    char * buf = calloc(filesize + 1, 1);  /* yay, already zeroed! */
    fread(buf, 1, filesize, fp);
    printf("File contents: '%s'\nFile content size: %u.\n", buf, strlen(buf));
    

    If you’re reading arbitrary (“binary”) files, this will generally not produce the expected result (unless you know what to expect).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <stdlib.h> int main ( int argc, char *argv[] ) { //sets
Please see this piece of code: #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { int i
I create a file using the code below: #include <stdio.h> #include <stdlib.h> #include <sys/stat.h>
see this code #include<stdio.h> int main() { void test(void) { printf(test); return; } printf(main);
This is the code: #include <stdio.h> #include <stdlib.h> void acceptMaze(char maze[ROW][COLOUMN], int nrows, int
I have the following code: #include <stdlib.h> #include <stdio.h> #define SIZE 100 int* arr;
Please see the code below: #include <iostream> #include <stdlib.h> #include <time.h> using namespace std;
folks please see following code : #include <gio/gio.h> #include <sys/socket.h> //socket(); #include <netinet/in.h> //sockaddr_in
#include stdafx.h #include stdio.h #include math.h #include stdlib.h void test (int a,int *b, int
The following code does not want to compile. See the included error message. Code:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.