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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:14:38+00:00 2026-06-07T02:14:38+00:00

I have this program which reads data from LBA (logical block address), but everytime

  • 0

I have this program which reads data from LBA (logical block address), but everytime whatever the LBA number i provide, it gives the same output.

How do i validate it?

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <linux/fs.h>
//#include "common.h"

typedef unsigned long long int var64;


int getSectorSize(int handle)
{
        int sectorSize = 0;

        //get the physical sector size of the disk
        if (ioctl(handle, BLKSSZGET, &sectorSize)) {

                printf("getSectorSize: Reading physical sector size failed.\n");

                sectorSize = 512;
        }

        return sectorSize;
}



var64 readLBA(int handle, var64 lba, void* buf, var64 bytes)
{
        int ret = 0;
        int sectorSize = getSectorSize(handle);
        var64 offset = lba * sectorSize;

        printf("readFromLBA: entered.\n");

        lseek64(handle, offset, SEEK_SET);
        ret = read(handle, buf, bytes);
        if(ret != bytes) {

              printf("read LBA: read failed.\n");

                return -1;
        }

        printf("read LBA: retval: %lld.\n", ret);
        return ret;
}

int main()
{
  int sectorSize, fd;
  char buff[100];
  printf("Calling getSectorSize\n");

  fd = open("/dev/sda1", O_RDONLY);

  if(fd == -1)
  {
    printf("open /dev/sda1 failed");
    exit(1);
  }
  sectorSize = getSectorSize(fd);
  printf("Sector size = %u\n", sectorSize);
  memset(buff, 0, sizeof(buff)); 
  readLBA(fd, 1, buff, 2); // if i put the 2nd arg as -12378 gives same answer
}

Here is the output:

sles10-sp3:~ # gcc getSectorSizeMain.c
getSectorSizeMain.c: In function ‘main’:
getSectorSizeMain.c:75: warning: incompatible implicit declaration of built-in function ‘memset’
sles10-sp3:~ # ./a.out
Calling getSectorSize
Sector size = 512
read LBA: entered.
read LBA: retval: 8589934594. // This is always constant, how to validate? If i tell to read an invalid LBA number like -123456 the answer remains same. How to validate?
  • 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-07T02:14:40+00:00Added an answer on June 7, 2026 at 2:14 am

    retval doesn’t contain the data you are interested in, but the count of bytes read() has stored into your buffer, so it’s natural it always contains the same value. But in your test output you try to print it using “%lld” (long long int), even when it’s just a plain int, so printf will combine its value with whatever it finds next to it on the stack (notice that 8589934594==0x200000002 – the last digit is your value, the first one propably garbage).

    The data you want to check/use/whatever are inside the array buff.

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

Sidebar

Related Questions

I have a program that reads arbitrary data from a file system and outputs
I am working on an addressbook program, which reads data from a csv file
I am required to create a program which reads in data from a .cvs
I have a C++ program which reads files from the hard disk and does
I have a spring batch program which reads from a file and writes to
I have written a program which reads a file containing multidimensional data (most commonly
I have this program in Python which should save text files to a folder
Alright, I have this program to sparse code in Newick Format, which extracts both
I have written this program, which sorts some ints using a functor: #include<iostream> #include<list>
I have a program which does a system call: latex somefile.latex This runs ok,

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.