fdopen

Correct way of using fdopen

大憨熊 提交于 2020-07-10 10:29:26
问题 I mean to associate a file descriptor with a file pointer and use that for writing. I put together program io.cc below: int main() { ssize_t nbytes; const int fd = 3; char c[100] = "Testing\n"; nbytes = write(fd, (void *) c, strlen(c)); // Line #1 FILE * fp = fdopen(fd, "a"); fprintf(fp, "Writing to file descriptor %d\n", fd); cout << "Testing alternate writing to stdout and to another fd" << endl; fprintf(fp, "Writing again to file descriptor %d\n", fd); close(fd); // Line #2 return 0; } I

Safety check prior to using fdopen

↘锁芯ラ 提交于 2020-06-09 07:07:04
问题 Motivated by Smart-write to arbitrary file descriptor from C/C++, I mean to associate a file descriptor with a file pointer and use that for writing. I put together program io.cc below: int main() { int nbytes; const int fd = 3; FILE * fp = fdopen(fd, "a"); fprintf(fp, "Writing to file descriptor %d\n", fd); cout << "Testing alternate writing to stdout and to another fd" << endl; fprintf(fp, "Writing again to file descriptor %d\n", fd); fclose(fp); return 0; } Should I perform some kind of

Safety check prior to using fdopen

限于喜欢 提交于 2020-06-07 07:22:26
问题 Motivated by Smart-write to arbitrary file descriptor from C/C++, I mean to associate a file descriptor with a file pointer and use that for writing. I put together program io.cc below: int main() { int nbytes; const int fd = 3; FILE * fp = fdopen(fd, "a"); fprintf(fp, "Writing to file descriptor %d\n", fd); cout << "Testing alternate writing to stdout and to another fd" << endl; fprintf(fp, "Writing again to file descriptor %d\n", fd); fclose(fp); return 0; } Should I perform some kind of

How to open file object from 'os' using 'with'?

一曲冷凌霜 提交于 2019-12-24 10:57:28
问题 I'm trying to open file using 'os.open()' as below >>> filePath 'C:\\Shashidhar\\text.csv' >>> fd = os.open(filePath,os.O_CREAT) >>> with os.fdopen(fd, 'w') as myfile: ... myfile.write("hello") IOError: [Errno 9] Bad file descriptor >>> Any idea how can I open the file object from os.fdopen using "with" so that connection can be closed automatially? Thanks 回答1: use this form, it worked. with os.fdopen(os.open(filepath,os.O_CREAT | os.O_RDWR ),'w') as fd: fd.write("abcd") 回答2: To elaborate on

Is it possible to rescue file descriptor from FILE*?

陌路散爱 提交于 2019-12-20 04:24:34
问题 I have to use a certain cross-platform library which passes FILE* objects around. I get a file descriptor from another source (inherited), I want to keep same fd across fork 'd processes. I currently use fdopen to convert a file descriptor to a FILE* object. My problem is that fclose used to clean up FILE* objects closes connected file descriptor. I would very much like to keep this file descriptor after it has been used. is there a way rescue file descriptor from FILE* ? Is there a way to

“Illegal seek” error when working with socket streams with non-empty read buffers

狂风中的少年 提交于 2019-12-10 13:33:48
问题 I'm currently writing a server application on Linux x86_64 using <sys/socket.h> . After accepting a connection via accept() , I use fdopen() to wrap the retrieved socket into a FILE* stream. Writing to, and reading from, that FILE* stream usually works quite well, but the socket becomes unsusable as soon as I write to it while it has a non-empty read buffer. For demonstration purposes, I've written some code that listens for a connection, then reads the input, line by line, into a read buffer

Create a file descriptor

坚强是说给别人听的谎言 提交于 2019-12-10 11:44:48
问题 I want to create a file descriptor in C whose value i will specify in code. I have a integer variable which specifies the value of file descriptor to be created. For example i may need a file descriptor whose value is 5 and later associate that with the file named "sample.dat" . 回答1: You need dup2() http://linux.die.net/man/2/dup 回答2: fd = open ("sample.dat", O_RDONLY); open the file dup2 (fd, 5); and copy the file descriptor fd into the descriptor number 5 now you can do read (5, buffer,

Create a file descriptor

冷暖自知 提交于 2019-12-06 15:12:48
I want to create a file descriptor in C whose value i will specify in code. I have a integer variable which specifies the value of file descriptor to be created. For example i may need a file descriptor whose value is 5 and later associate that with the file named "sample.dat" . You need dup2() http://linux.die.net/man/2/dup fd = open ("sample.dat", O_RDONLY); open the file dup2 (fd, 5); and copy the file descriptor fd into the descriptor number 5 now you can do read (5, buffer, BUFF_MAX); or also use fd to access the same file. You need to close the fd explicitly if you do not need it. As

Python - How do I convert “an OS-level handle to an open file” to a file object?

风格不统一 提交于 2019-12-03 06:13:35
问题 tempfile.mkstemp() returns: a tuple containing an OS-level handle to an open file (as would be returned by os.open()) and the absolute pathname of that file, in that order. How do I convert that OS-level handle to a file object? The documentation for os.open() states: To wrap a file descriptor in a "file object", use fdopen(). So I tried: >>> import tempfile >>> tup = tempfile.mkstemp() >>> import os >>> f = os.fdopen(tup[0]) >>> f.write('foo\n') Traceback (most recent call last): File "

Is it possible to rescue file descriptor from FILE*?

雨燕双飞 提交于 2019-12-02 05:45:17
I have to use a certain cross-platform library which passes FILE* objects around. I get a file descriptor from another source (inherited), I want to keep same fd across fork 'd processes. I currently use fdopen to convert a file descriptor to a FILE* object. My problem is that fclose used to clean up FILE* objects closes connected file descriptor. I would very much like to keep this file descriptor after it has been used. is there a way rescue file descriptor from FILE* ? Is there a way to detach it? Or a way to substitute a file descriptor in FILE* with a dummy? P.S. this needs to be cross