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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:13:06+00:00 2026-06-17T06:13:06+00:00

I wrote this small code to ascertain read behavior. #include <stdio.h> #include <sys/types.h> #include

  • 0

I wrote this small code to ascertain read behavior. 

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>

int main ()
{
  ssize_t ret;
  int fd;
  char str[30] = {0};
  off_t lret

  fd = open("./sample", O_RDWR);
  printf("File descriptor = %d\n",fd);

  lret = lseek(fd,LONG_MAX,SEEK_SET);
  printf("%ld\n",lseek(fd, 0, SEEK_CUR));

  ret = read(fd, str, 20);
  if (ret == -1) {
     perror("read error");
  }
  else {
     printf("%ld\n",ret);
     printf("%s\n",str);
  }

  ret = write(fd, "bye", 3);
  if (ret == -1) {
     perror("write error");
  }
  else
     printf("%ld\n",ret);

  printf("%ld\n",lseek(fd, 0, SEEK_CUR));
  close (fd);

  return 0;
}

Here is the output:

$ cat sample
HELLO$ ./a.out
File descriptor = 3
4294967295
read error: Invalid argument
write error: Invalid argument
4294967295
$ ll sample
-rw-r--r--. 1 bruce stud 5 Jan 14 17:25 sample

But if I change lseek statement to

ret = lseek(fd,5,SEEK_SET);

read returns 0

$ ./a.out
File descriptor = 3
5
0

3
8
$ cat sample
HELLObye$ ll sample
-rw-r--r--. 1 bruce stud 8 Jan 14 17:26 sample

Why does read behave like this?

  • 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-17T06:13:07+00:00Added an answer on June 17, 2026 at 6:13 am

    Note that the value returned by lseek is a off_t, not a size_t. The difference is that off_t is signed. When you take a signed value and make it unsigned, it appears like a large positive number.

    I would expect that “LONG_MAX” is not actually 4294967295, but either 2147483647 (2^31-1) or a much larger number. So the 4294967295 comes from a -1 [it is 2^32-1, which is indeed the same as -1 in 32-bit math].

    In other words, you are getting an error from lseek.

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

Sidebar

Related Questions

I wrote this small piece of code to test something: #include <stdio.h> int main()
I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
I wrote this program: #include <stdio.h> /*Part B Write a program that: defines an
I wrote this small piece of code in C to test memcmp() strncmp() strcmp()
I have this small snippet of python code that I wrote. It works, but
I read this and wrote this code: .wholePageDivForCentering { width: 80%; white-space: nowrap; display:inline-block;
I wrote this small code just to see how an iterator actually gets invalidated
I wrote this small bit of code to write to a plist file. I
I wrote very small code for java script.IN this value of message box are
I wrote this code for zoom in/out . it works but even with one

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.