Hi all..

I want to know the file descriptor of a file that I have opened in my program.

I know open(char*,int,int) returns a file descriptor.

What if use fstream out("...").. Is there any way to know the file descriptor this way??

thnx.

Dani AI

Generated

was right that this is an OS-level detail and the C++ standard does not promise a file-descriptor accessor on streams. Practical advice for the mmap use case:

  • Prefer opening the file with the platform API you will use for mmap, then map that descriptor. That keeps intent clear and avoids relying on nonstandard internals of std::filebuf (the standard API does not expose an FD) — see the filebuf remarks on cppreference for implementation notes (basic_filebuf).

  • If you must map an already-opened stream, be aware any method to extract a descriptor is implementation-specific and not portable.

Common causes of the SIGBUS / "bus error" you saw and how to check them:

  • mmap failed: always check mmap return against MAP_FAILED and examine errno. See mmap(2).
  • Wrong protections or open flags: mapping with PROT_WRITE/MAP_SHARED requires the file opened for writing; mismatches can fail or misbehave.
  • Mapping past end of file: mapping or writing past the file length can cause SIGBUS. Use fstat to get the file size and ftruncate to extend the file before mapping if you need to write past its current end (ftruncate(2)).
  • Alignment/architecture issues: on some CPUs unaligned accesses cause SIGBUS. Avoid direct unaligned dereferences; use memcpy or access as bytes if portability matters.
  • Using the pointer after munmap or after intentionally altering the mapped file size will crash.

Minimal checklist before using the returned pointer:

  • Confirm mmap did not return MAP_FAILED.
  • Confirm mapping length > 0 and within/extended to the file size.
  • Confirm open mode matches requested protections.
  • Verify you are not performing unaligned accesses on strict-ABI hardware.

These steps will catch the majority of mmap-related SIGBUS problems. For concrete examples and diagnostics, the linked man pages provide authoritative error behaviors and flags.

Recommended Answers

All 6 Replies

What would you do with the information if you had it?

(: [...I'm not gonna take over the world with it..!!]

I want to try out mmap() function.. and it requires me to specify a file descriptor to the file which is to be mapped...

so, if I had the information, I'll put it inside mmap()..

But mmap() is not part of C++. For that matter, neither are file descriptors. So what you're really saying is that you want to use information that your particular implementation provides in an implementation-dependent way.

Which means that the answer to your question relies on the details of your implementation.

ok...
is there any specific reason that file descriptors are not a part of c++??

and..if i want to do something like mmap() in c++, what should i do?

ok...
is there any specific reason that file descriptors are not a part of c++??

and..if i want to do something like mmap() in c++, what should i do?

File descriptors are not part of C++ because if they were, how would C++ be implemented on an operating system that does not have them? Ditto mmap().

So if you want to use such facilities, you have to consult the documentation for your particular implementation and find out how, and whether, the implementation has made them available.

ok...
also.. I tried implementing mmap() in a c program..
mmap() runs fine... but when I try using the corresponding pointer it returns, i get bus error..
how can i fix this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.