Re: lseek: Value too large for defined data type



Hi,

I got the problem. I was not included the header file
#include<unistd.h>. after including this i will returning correct
value. so it is running perfectly.

But one more clarification.

But if I dont include this header file, it should give warning?. and
How, including of this header file affecting the return value of
lseek.

Thanks for the help. i really learnt a lot from this.
-Gururaja

guru wrote:
Hi,

I got it. I am able to run this program.
But i am seeing difference in output of the lseek. After seeking 1024
bytes, i am printing the value returned by the lseek. it is always
printing 1024 bytes only.
But according to lseek, it should return number of bytes starting from
the beginning of the file.

What is making it to return always 1024 but not the actual value.

Thanks
Gururaj



Tim Southerwood wrote:
guru wrote:

I am unable to seek. again same error it is giving.

First i will write 21GB then run the below program

The program

#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
main()
{

int file = 0;
char *buff = NULL;
int bytewr,count=0,loopcount,choice;
long long seekOffset = 0;

buff = (char *)malloc(1024 * 1024);
if(buff == NULL)
{
printf("Memory Allocation failed\n");
return -1;
}
else
{
printf("Memory allocation success\n");
}

file = open("/HDR1/test1.txt", O_LARGEFILE|O_RDWR);
if(file == -1)
{
printf("Error opening file\n");
return -1;
}
else
{
printf("File opening success\n");
}
/*1MB seek*/
seekOffset = lseek(file,(1024*1024),SEEK_CUR);
if(seekOffset == -1)
{
printf("Seek Fail\n");
return -1;
}
else
{
printf("Seek Success=%d\n",seekOffset);
}

memset(buff,0x44,(1024*1024));
loopcount = 10240; /*do it for this many times*/
{
count = 0;
do {
bytewr = write(file, buff,(1024*1024));
if(bytewr == (1024*1024))
{
printf("Count: %d Write Success\n",count); }
else
{
printf("Write fail\n");
return -1;
}
if((count%100) == 0)
{
/*Seek afterevery
100MB*/
seekOffset = lseek(file,(1024),SEEK_CUR);
if(seekOffset == -1)
{
printf("Seek Fail\n");
return -1;
}
else
{
printf("Seek Success=%Ld\n",seekOffset);
}
}

}while(count++ < loopcount);
close(file);
free(buff);
}

while compiling i have to give

gcc -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE append.c -o append

after 2100 loop, it is failing.

is there any problem with application.

Regards
Gururaja

Hi,

It works if you apply this patch:

% diff -u append.c.orig append.c
--- append.c.orig 2007-07-24 09:48:17.857169930 +0100
+++ append.c 2007-07-24 10:25:21.523034930 +0100
@@ -11,7 +11,7 @@
int file = 0;
char *buff = NULL;
int bytewr,count=0,loopcount,choice;
- long long seekOffset = 0;
+ off_t seekOffset = 0;

buff = (char *)malloc(1024 * 1024);
if(buff == NULL)
@@ -24,7 +24,7 @@
printf("Memory allocation success\n");
}

- file = open("/tmp/test1.txt", O_LARGEFILE|O_RDWR);
+ file = open("/tmp/test1.txt", O_RDWR);
if(file == -1)
{
printf("Error opening file\n");
@@ -35,7 +35,7 @@
printf("File opening success\n");
}
/*1MB seek*/
- seekOffset = lseek(file,(1024*1024),SEEK_CUR);
+ seekOffset = lseek(file,(off_t)(1024*1024),SEEK_CUR);
if(seekOffset == -1)
{
printf("Seek Fail\n");
@@ -63,7 +63,7 @@
if((count%100) == 0)
{
/*Seek afterevery 100MB*/
- seekOffset = lseek(file,(1024),SEEK_CUR);
+ seekOffset = lseek(file,(off_t)(1024),SEEK_CUR);
if(seekOffset == -1)
{
printf("Seek Fail\n");

##########################################################

The first line is not correct, though it doesn't break due to "long long"
being good enough in this case, but you really need to follow the header
definitions faithfully otherwise random bad things will happen in the
future. lseek returns an off_t, so that's what you should use.

The second line replacement is just that O_LARGEFILE is not needed, it is
brought in by the -D options.

The latter two lines indicate that you have an implicit problem with the
compiler casting your int's to the correct this - off_t (which will be
off64_t by them due to the -D options.

Explicitly casting fixes it quite nicely. Personally I might explicitly cast
the "== -1" to "== (off_t) -1" just for clarity, but I suspect the compiler
is able to infer that reliably from seekOffset and do the right thing.

HTH

Tim

.



Relevant Pages

  • Re: lseek: Value too large for defined data type
    ... But i am seeing difference in output of the lseek. ... i am printing the value returned by the lseek. ... printf("File opening success\n"); ...
    (comp.os.linux.development.system)
  • Re: Porting a C++ form from VS2003 to VS2005
    ... In VS2003 the form shows up under my header files for the project with a special dark icon for the header file and another special icon under it for the .resX file. ... I had to separately add the .resX file to the project but the header file and its .resX file are totally disconnected in the IDE. ... Opening the header file just brings up its code and opening the .resX file just beings up an editor for .resX files with an Other, Add Resource, and Move Resource menu items. ...
    (microsoft.public.vsnet.ide)
  • why is it called "asm-generic/atomic.h"?
    ... its opening comment reads: ... Allows to provide arch independent atomic definitions without the need to ... but that's *not* what that header file does. ... Linux Consulting, ...
    (Linux-Kernel)