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

  • Home
  • SEARCH
  • 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 8159783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:02:20+00:00 2026-06-06T18:02:20+00:00

if I read file by calling read() like this: unsigned char buf[512]; memset(buf, 0,

  • 0

if I read file by calling read() like this:

unsigned char buf[512];
memset(buf, 0, sizeof(unsigned char) * 512);
int fd;
int readcount;
int offset = 10315001; /* file size is 14315504 */

fd = open("myfile", O_RDONLY);
lseek(fd, offset, SEEK_SET);
readcount = read(fd, (void*)buf, 8);
close(fd);

the read() returns 0, but the memory of “buf” has been changed(not 0 any more). And if I tried to read the same offset and same file with fread() like this:

FILE* file;
file = fopen("myfile", "r");
fseek(file, offset, SEEK_SET);
readcount = fread((void*)buf, 8, 1, file);
fclose(file);

fread() returns 0 either, but the buf is as same as before.

if read() failed, why it changes the memory space of “buf”? Or did I make some mistakes?

Thanks for help : )

Edit: Every time I ran the code above, the “buf” changed by read() in the same way -> from 0 to the same values. So the changed “buf” may not be random values?

Edit 2: The offset parameter is valid(thanks twalberg), and if I read another valid offset, both read() and fread() will be succeeded and the result of “buf” is the same. Is there any way to find what’s wrong when read() failed? The errno is “No errors” when read() returns 0.

  • 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-06T18:02:22+00:00Added an answer on June 6, 2026 at 6:02 pm

    I think I’ve found what’s going on here.
    The file is a binary one, but I read() it with text mode(O_RDONLY).

    The value of offset 10315001 is 0x1a, when read() and fread() function meets 0x1a in text mode, they both will return 0, but the different is, the read() will still write the buf with binary mode, while fread() won’t do this.

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

Sidebar

Related Questions

I'have a bash script calling a jar file like this: awk -f fileformat.awk list.txt
How to read strings from the MyResource.resx file from c#. I am not calling
I read file, but in the end of file i get unknown symbols: int
I'm having problems reading this one JPEG file using ImageIO.read(File file) - it throws
The usual way to read a file in C++ is this one: std::ifstream file(file.txt,
I have a sample xml file that looks like this: <Books> <Category Genre=Fiction BookName=book_name
I am calling a method from C# like this: [DllImport(@C:\Hash.dll, CallingConvention = CallingConvention.Cdecl)] public
I'm calling a method from an external library with a (simplified) signature like this:
I am trying to read file headers using java, I want to get file
I try to read file /proc/'pid'/status, using c program. The code is as follows,

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.