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.
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.
— 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 return against MAP_FAILED and examine errno. See mmap(2).PROT_WRITE/MAP_SHARED requires the file opened for writing; mismatches can fail or misbehave.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)).memcpy or access as bytes if portability matters.munmap or after intentionally altering the mapped file size will crash.Minimal checklist before using the returned pointer:
mmap did not return MAP_FAILED.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.
Jump to Post— arkoenig 340What would you do with the information if you had it?
Jump to Post— arkoenig 340But 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 …
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?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.