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 8989787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:20:50+00:00 2026-06-15T22:20:50+00:00

I want to read 80 characters from a text file, save them to a

  • 0

I want to read 80 characters from a text file, save them to a string and then print that string for verification.
I am using fread and have this code:

char message[80];
size_t nread = fread(message, 1, sizeof(message), fp);
printf("Message received: \"__%s__\"\n", message);

And the output is:

Message received: “__I would love to change the world but they won’t
��__”

when the whole phrase isn’t in the file

and:

__I would love to change the world but they won’t give me the source code
@__

when the whole phrase is in the file.

I’ve tried deleting it and rewriting the text but I get the same output again.

What is wrong? Something with the charset?

  • 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-06-15T22:20:52+00:00Added an answer on June 15, 2026 at 10:20 pm

    fread() does not null terminate the buffer and prinf("%s") expects the buffer to be null terminated. As the buffer is not null terminated printf() will continue reading until it finds a null terminator, printing out any junk along the way.

    Either null terminate the buffer, remembering to leave room for the null terminator:

    char message[81]; /* Increased by 1 for null terminator. */
    size_t nread = fread(message, 1, sizeof(message) - 1, fp);
    message[nread] = 0;
    printf("Message received: \"__%s__\"\n", message);
    

    or instruct printf() to only print the number of characters read using "%.*s" format specifier:

    printf("Message received: \"__%.*s__\"\n", nread, message);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to read characters from the console and print them one after another
I want to read data about MMORPG characters from a .txt file and then
I have a text file. I want read that file. But In that if
I want to read text from a text file line by line and do
I want to read some specific information from a .txt file and display that
I need to take a string that was originally read in from a text
What I want to do is read a line from text file which contains
I'm working with a .txt file. I want a string of the text from
I have a text file I want to read, but exclude lines that contain
I want to read a INI file from python, and I came up with

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.