Products > Programming

Open large files on 32-bit OS and ANSI C

<< < (4/4)

Karel:

--- Quote from: Nominal Animal on August 29, 2023, 11:56:33 am ---
--- Quote from: Karel on August 29, 2023, 11:35:28 am ---Using this I can use fopeno(), fseeko() and ftello() on most common OS'es and with most common compilers, right?

--- End quote ---
Won't work in glibc-based systems (many/most Linux ones) without modifications.

--- End quote ---

I tried and it seems to work fine on both Linux and windows with different compilers.

Nominal Animal:

--- Quote from: Karel on August 29, 2023, 12:24:45 pm ---
--- Quote from: Nominal Animal on August 29, 2023, 11:56:33 am ---
--- Quote from: Karel on August 29, 2023, 11:35:28 am ---Using this I can use fopeno(), fseeko() and ftello() on most common OS'es and with most common compilers, right?

--- End quote ---
Won't work in glibc-based systems (many/most Linux ones) without modifications.

--- End quote ---
I tried and it seems to work fine on both Linux and windows with different compilers.

--- End quote ---
Enable compiler warnings, then.  Or you didn't show all the #defines and #includes you used.

Karel:

--- Quote from: Nominal Animal on August 29, 2023, 12:29:46 pm ---
--- Quote from: Karel on August 29, 2023, 12:24:45 pm ---
--- Quote from: Nominal Animal on August 29, 2023, 11:56:33 am ---
--- Quote from: Karel on August 29, 2023, 11:35:28 am ---Using this I can use fopeno(), fseeko() and ftello() on most common OS'es and with most common compilers, right?

--- End quote ---
Won't work in glibc-based systems (many/most Linux ones) without modifications.

--- End quote ---
I tried and it seems to work fine on both Linux and windows with different compilers.

--- End quote ---
Enable compiler warnings, then.  Or you didn't show all the #defines and #includes you used.

--- End quote ---


--- Code: ---#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

--- End code ---

In the makefile:


--- Code: ---CC = gcc
CFLAGS = -O2 -Wall -Wextra -Wshadow -Wformat-nonliteral -Wformat-security -D_LARGEFILE64_SOURCE -D_LARGEFILE_SOURCE
LDLIBS = -lm

--- End code ---

Sorry, I forgot to mention the compiler options  :-[

Nominal Animal:
Okay – although I'd add -D_FILE_OFFSET_BITS=64 too as _LARGEFILE_SOURCE is obsolete for glibc –, but I still think you'd be better off using the POSIX.1 subset, using the same preprocessor logic.  Something along the lines of

--- Code: ---#ifndef   POSIX_IO_H
#define   POSIX_IO_H

// Define USE_CUSTOM_GETLINE to use custom getline() implementation
#undef  NEED_CUSTOM_GETLINE

// Define USE_CUSTOM_GETDELIM to use custom getdelim() implementation
#undef  NEED_CUSTOM_GETDELIM

// Operating systems:
#if defined(_WIN32)
#if defined(__MINGW32__)
// 32-bit MingW on Windows

// TODO

#elif defined(__MINGW64__)
// 64-bit MingW on Windows

// TODO

#elif defined(_WIN64)
// 64-bit Windows

// TODO

#else
// 32-bit Windows

// TODO

#endif
// Microsoft Visual C Runtime does not implement getline(), getdelim()
#define  NEED_CUSTOM_GETLINE
#define  NEED_CUSTOM_GETDELIM

#elif defined(__APPLE__) && defined(__MACH__)
// Mac OS

// At least Darwin 10.5 and later use 64-bit off_t by default
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__)
// BSD variants

// Always 64-bit off_t, with fseeko() and ftello() defined in sys/types.h
#include <stdlib.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#elif defined(__HAIKU__)
// Haiku

// POSIX.1 compatible by default, and Haiku and BeOS off_t was always 64-bit.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#elif defined(__linux__)
// Linux and Android

// Legacy definitions
#ifndef _LARGEFILE_SOURCE
#define _LARGEFILE_SOURCE
#endif
#ifndef _LARGEILE64_SOURCE
#define _LARGEFILE64_SOURCE
#endif

// Ensure 64-bit off_t
#undef _FILE_OFFSET_BITS
#define _FILE_OFFSET_BITS 64

// Include POSIX.1 C interfaces
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200809L
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#endif /* Operating systems */

#if defined(NEED_CUSTOM_GETLINE) || defined(USE_CUSTOM_GETLINE)

// Custom getline() implementation
static ssize_t custom_getline(char **lineptr, size_t *size, FILE *stream)
{
   // ...
}
#undef  getline
#define  getline  custom_getline
#endif

#if defined(NEED_CUSTOM_GETDELIM) || defined(USE_CUSTOM_GETDELIM)

// Custom getdelim() implementation
static ssize_t custom_getdelim(char **lineptr, size_t *size, int delim, FILE *stream)
{
    if (delim == '\n')
        return custom_getline(lineptr, size, stream);

   // ...
}
#undef  getdelim
#define  getdelim  custom_getdelim
#endif

#endif /* POSIX_IO_H */

--- End code ---
but the above is untested.

This way, your source code only needs to start with #include "posix-io.h", and use the POSIX.1 interfaces (ftello(), fseeko(), and otherwise standard C99 or later <stdio.h>, <string.h>, and <errno.h> stuff), and you can omit the -D command-line options.  This then also works with clang, too, as well as other C compilers (including Intel Compiler Collection), and possibly even MS C++ in C "mode".

westfw:

--- Quote --- if I want to write portable code that allows me to open large files also on 32-bit systems,
--- End quote ---


Are you sure that your "32bit system" OPERATING SYSTEM and DISK FORMAT support "large files"?
For instance, FAT32 is limited to 4GB-1, and HFS apparently 2GB.

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod