Re: lseek: Value too large for defined data type
- From: guru <guru.naveen@xxxxxxxxx>
- Date: Wed, 25 Jul 2007 06:29:21 -0000
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
.
- Follow-Ups:
- Re: lseek: Value too large for defined data type
- From: David Schwartz
- Re: lseek: Value too large for defined data type
- References:
- lseek: Value too large for defined data type
- From: guru
- Re: lseek: Value too large for defined data type
- From: spacecriter \(Bill C\)
- Re: lseek: Value too large for defined data type
- From: guru
- Re: lseek: Value too large for defined data type
- From: Tim Southerwood
- Re: lseek: Value too large for defined data type
- From: guru
- lseek: Value too large for defined data type
- Prev by Date: Re: lseek: Value too large for defined data type
- Next by Date: Re: lseek: Value too large for defined data type
- Previous by thread: Re: lseek: Value too large for defined data type
- Next by thread: Re: lseek: Value too large for defined data type
- Index(es):
Relevant Pages
|
|