All of lore.kernel.org
 help / color / mirror / Atom feed
* Re: How to lseek the larger file > 2GB under linux
       [not found] <d13fb4990905170840j6ac84b0ej758582e5ffb5fa8c@mail.gmail.com>
@ 2009-05-17 15:42 ` Nicle
  2009-05-17 16:20   ` Michal Nazarewicz
                     ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Nicle @ 2009-05-17 15:42 UTC (permalink / raw)
  To: linux-c-programming

Hi all,

 I have a file > 2GB, and my job is seeking the file to pos: 2.1G.
But, it seems that the lseek64 doesn't work.
Here is the sample code:

#define _LARGEFILE64_SOURCE
#include ...

int main()
{
 int fd = -1;
 long long pos = (long long) 2*1024*1024*1024 + 10; // over 2G

 fd = open(FILENAME, O_WRONLY|O_LARGEFILE);
 if (fd < 0){
   ...
 }

 if (lseek64(fd, pos, SEEK_SET) < 0) {
     fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
 }

 return 0;
}

 Then the building cmd:
    gcc -o test test.c -D_FILE_OFFSET_BITS=64

 Output:
    Failed seeking to 2147483658, Success.

The return val of lseek64  was "<0", but the strerror told me "Success".

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to lseek the larger file > 2GB under linux
  2009-05-17 15:42 ` How to lseek the larger file > 2GB under linux Nicle
@ 2009-05-17 16:20   ` Michal Nazarewicz
       [not found]     ` <d13fb4990905172137l1a4a9c92k47988da28ad4b7b1@mail.gmail.com>
  2009-05-17 18:10   ` LDB
  2009-05-17 20:09   ` Glynn Clements
  2 siblings, 1 reply; 5+ messages in thread
From: Michal Nazarewicz @ 2009-05-17 16:20 UTC (permalink / raw)
  To: Nicle; +Cc: linux-c-programming

[-- Attachment #1: Type: text/plain, Size: 1676 bytes --]

Nicle <ynicle@gmail.com> writes:
> I have a file > 2GB, and my job is seeking the file to pos: 2.1G.
> But, it seems that the lseek64 doesn't work.

> #define _LARGEFILE64_SOURCE
> #include ...
>
> int main() {
>  int fd = -1;
>  long long pos = (long long) 2*1024*1024*1024 + 10; // over 2G

A long shot, but try (2LL << 30) (the "LL" is important).  I don't
expect that will make it work but if you're out of ideas... ;)

>  fd = open(FILENAME, O_WRONLY|O_LARGEFILE);
>  if (fd < 0) { /* ... */ }
>
>  if (lseek64(fd, pos, SEEK_SET) < 0)
>      fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
>
>  return 0;
> }

>  Then the building cmd:   gcc -o test test.c -D_FILE_OFFSET_BITS=64
>  Output:            >     Failed seeking to 2147483658, Success.
>
> The return val of lseek64  was "<0", but the strerror told me "Success".

BTW. The following works fine for me:

#v+
#define _LARGEFILE64_SOURCE

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

#define RUN(expr) if ((expr) < 0) { perror(#expr); return 1; } else (void)0

int main(void) {
	const long long pos = (2LL << 30) + 10;
	int fd;

	RUN(fd = open("deleteme", O_WRONLY | O_LARGEFILE | O_CREAT, 0600));
	RUN(lseek64(fd, pos, SEEK_SET));
	RUN(write(fd, "a", 1));
	return 0;
}
#v-

-- 
Best regards,                                         _     _
 .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
 ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
 ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to lseek the larger file > 2GB under linux
  2009-05-17 15:42 ` How to lseek the larger file > 2GB under linux Nicle
  2009-05-17 16:20   ` Michal Nazarewicz
@ 2009-05-17 18:10   ` LDB
  2009-05-17 20:09   ` Glynn Clements
  2 siblings, 0 replies; 5+ messages in thread
From: LDB @ 2009-05-17 18:10 UTC (permalink / raw)
  To: Nicle; +Cc: linux-c-programming

Nicle wrote:
> Hi all,
> 
>  I have a file > 2GB, and my job is seeking the file to pos: 2.1G.
> But, it seems that the lseek64 doesn't work.
> Here is the sample code:
> 
> #define _LARGEFILE64_SOURCE
> #include ...
> 
> int main()
> {
>  int fd = -1;
>  long long pos = (long long) 2*1024*1024*1024 + 10; // over 2G
> 
>  fd = open(FILENAME, O_WRONLY|O_LARGEFILE);
>  if (fd < 0){
>    ...
>  }
> 
>  if (lseek64(fd, pos, SEEK_SET) < 0) {
>      fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
>  }
> 
>  return 0;
> }
> 
>  Then the building cmd:
>     gcc -o test test.c -D_FILE_OFFSET_BITS=64
> 
>  Output:
>     Failed seeking to 2147483658, Success.
> 
> The return val of lseek64  was "<0", but the strerror told me "Success".
> --
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> 
> 



I changed the below a little, but when I seeked to 2.1GB,
it worked fine. I am not writing like you, but reading.

#define _LARGEFILE64_SOURCE

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>

int main()
{
 int fd, ret;
 size_t count = 200;
 long long pos = (long long) 2*1024*1024*1024 + 10; // over 2G
 char buf[count];

 fd = open("/tmp/fish", O_RDONLY|O_LARGEFILE);
 if (fd < 0){
   fprintf(stderr, "%s\n", strerror(errno));
   exit(1);
 }

 if (lseek64(fd, pos, SEEK_SET) < 0) {
   fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
   exit(1);
 }

 if ((ret = read(fd, buf, count)) < 0) {
   fprintf(stderr, "Failed reading: %s\n", strerror(errno));
   exit(1);
 }

 buf[ret] = 0x00;
 printf("%s\n", buf);

 close(fd);

 return 0;
}

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to lseek the larger file > 2GB under linux
  2009-05-17 15:42 ` How to lseek the larger file > 2GB under linux Nicle
  2009-05-17 16:20   ` Michal Nazarewicz
  2009-05-17 18:10   ` LDB
@ 2009-05-17 20:09   ` Glynn Clements
  2 siblings, 0 replies; 5+ messages in thread
From: Glynn Clements @ 2009-05-17 20:09 UTC (permalink / raw)
  To: Nicle; +Cc: linux-c-programming


Nicle wrote:

>  I have a file > 2GB, and my job is seeking the file to pos: 2.1G.
> But, it seems that the lseek64 doesn't work.
> Here is the sample code:

[snip]

The following complete program works fine for me:

----------------------------------------------------------------------
#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define FILENAME "test.dat"

int main(void)
{
    int fd = -1;
    long long pos = (long long) 2*1024*1024*1024 + 10;

    fd = open(FILENAME, O_CREAT|O_WRONLY|O_LARGEFILE, 0666);
    if (fd < 0) {
	perror("open");
	return 1;
    }

    if (lseek64(fd, pos, SEEK_SET) < 0) {
	fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
	return 1;
    }

    return 0;
}
----------------------------------------------------------------------

In general, you should post complete programs which can be compiled
without needing to "fill in the blanks", as the problem may lie in
something which was omitted.

> The return val of lseek64  was "<0", but the strerror told me "Success".

The return value is an off64_t; ensure that it isn't being truncated
to an int, because that will result in a negative value.
 
In the above, the 0 on the RHS of the comparison should be promoted to
an off64_t automatically (if it isn't, that's a bug in the compiler).

-- 
Glynn Clements <glynn@gclements.plus.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: How to lseek the larger file > 2GB under linux
       [not found]     ` <d13fb4990905172137l1a4a9c92k47988da28ad4b7b1@mail.gmail.com>
@ 2009-05-18  4:38       ` Nicle
  0 siblings, 0 replies; 5+ messages in thread
From: Nicle @ 2009-05-18  4:38 UTC (permalink / raw)
  To: Michal Nazarewicz; +Cc: linux-c-programming

Hi Michal,

The "LL" does make my prog work :)

Thanks for your help!!

Best Regard
Nicle Yang


>
>
> 2009/5/18 Michal Nazarewicz <mina86@tlen.pl>
>>
>> Nicle <ynicle@gmail.com> writes:
>> > I have a file > 2GB, and my job is seeking the file to pos: 2.1G.
>> > But, it seems that the lseek64 doesn't work.
>>
>> > #define _LARGEFILE64_SOURCE
>> > #include ...
>> >
>> > int main() {
>> >  int fd = -1;
>> >  long long pos = (long long) 2*1024*1024*1024 + 10; // over 2G
>>
>> A long shot, but try (2LL << 30) (the "LL" is important).  I don't
>> expect that will make it work but if you're out of ideas... ;)
>>
>> >  fd = open(FILENAME, O_WRONLY|O_LARGEFILE);
>> >  if (fd < 0) { /* ... */ }
>> >
>> >  if (lseek64(fd, pos, SEEK_SET) < 0)
>> >      fprintf(stderr, "Failed seeking to %lld, %s\n", pos, strerror(errno));
>> >
>> >  return 0;
>> > }
>>
>> >  Then the building cmd:   gcc -o test test.c -D_FILE_OFFSET_BITS=64
>> >  Output:            >     Failed seeking to 2147483658, Success.
>> >
>> > The return val of lseek64  was "<0", but the strerror told me "Success".
>>
>> BTW. The following works fine for me:
>>
>> #v+
>> #define _LARGEFILE64_SOURCE
>>
>> #include <errno.h>
>> #include <fcntl.h>
>> #include <stdio.h>
>> #include <string.h>
>> #include <sys/stat.h>
>> #include <sys/types.h>
>> #include <unistd.h>
>>
>> #define RUN(expr) if ((expr) < 0) { perror(#expr); return 1; } else (void)0
>>
>> int main(void) {
>>        const long long pos = (2LL << 30) + 10;
>>        int fd;
>>
>>        RUN(fd = open("deleteme", O_WRONLY | O_LARGEFILE | O_CREAT, 0600));
>>        RUN(lseek64(fd, pos, SEEK_SET));
>>        RUN(write(fd, "a", 1));
>>        return 0;
>> }
>> #v-
>>
>> --
>> Best regards,                                         _     _
>>  .o. | Liege of Serenly Enlightened Majesty of      o' \,=./ `o
>>  ..o | Computer Science,  Michal "mina86" Nazarewicz   (o o)
>>  ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
>
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-05-18  4:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <d13fb4990905170840j6ac84b0ej758582e5ffb5fa8c@mail.gmail.com>
2009-05-17 15:42 ` How to lseek the larger file > 2GB under linux Nicle
2009-05-17 16:20   ` Michal Nazarewicz
     [not found]     ` <d13fb4990905172137l1a4a9c92k47988da28ad4b7b1@mail.gmail.com>
2009-05-18  4:38       ` Nicle
2009-05-17 18:10   ` LDB
2009-05-17 20:09   ` Glynn Clements

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.