logo
down
shadow

How should I manage ::std::cout after changing file descriptor 1 to refer to a different file?


How should I manage ::std::cout after changing file descriptor 1 to refer to a different file?

By : Eric Henson
Date : October 18 2020, 01:08 AM
it helps some times Option 1: set stdin & stdout
According to cppreference.com:
code :
if (freopen("input.txt", "r", stdin) == NULL) {
    // Handle error, errno is set to indicate error
}

if (freopen("output.txt", "w", stdout) == NULL) {
    // Handle error, errno is set to indicate error
}
std::ifstream fin("input.txt");
if (!fin) {
    // Handle error
}
cin.rdbuf(fin.rdbuf());

std::ofstream fout("output.txt");
if (!fout) {
    // Handle error
}
cout.rdbuf(fout.rdbuf());
boost::asio::ip::tcp::iostream stream("www.boost.org", "http");
if (!stream) {
    // Handle error
}

cin.rdbuf(stream.rdbuf());
cout.rdbuf(stream.rdbuf());

// GET request example

cout << "GET /LICENSE_1_0.txt HTTP/1.0\r\n";
cout << "Host: www.boost.org\r\n";
cout << "Accept: */*\r\n";
cout << "Connection: close\r\n\r\n";
cout.flush();

std::string response;
std::getline(cin, response);


Share : facebook icon twitter icon
how to use isatty() on cout, or can I assume that cout == file descriptor 1?

how to use isatty() on cout, or can I assume that cout == file descriptor 1?


By : Siri Sander
Date : March 29 2020, 07:55 AM
this will help It is possible to use rdbuf() to change the destination of std::cin and std::cout inside your program. If you don't do that, it is probably quite safe to assume that cin = 0, cout=1 and clog and cerr both = 2 as the C++ standard states that they are synchronized with C stdin, stdout and stderr and those have per POSIX those file descriptors at startup.
Can I check if two FILE* or file descriptor numbers refer to the same file?

Can I check if two FILE* or file descriptor numbers refer to the same file?


By : Gagan
Date : March 29 2020, 07:55 AM
Any of those help Yes - compare the file device ID and inode. Per the specification:
code :
int same_file(int fd1, int fd2) {
    struct stat stat1, stat2;
    if(fstat(fd1, &stat1) < 0) return -1;
    if(fstat(fd2, &stat2) < 0) return -1;
    return (stat1.st_dev == stat2.st_dev) && (stat1.st_ino == stat2.st_ino);
}
smart pointer to manage socket file descriptor

smart pointer to manage socket file descriptor


By : user3475013
Date : March 29 2020, 07:55 AM
Hope this helps Because fd isn't a pointer, I wouldn't try to pigeonhole it into unique_ptr. Instead, create a custom class whose interface is based on unique_ptr, as in (caution: totally untested):
code :
class unique_fd
{
public:
    constexpr unique_fd() noexcept = default;
    explicit unique_fd(int fd) noexcept : fd_(fd) {}
    unique_fd(unique_fd&& u) noexcept : fd_(u.fd_) { u.fd_ = -1; }

    ~unique_fd() { if (-1 != fd_) ::close(fd_); }

    unique_fd& operator=(unique_fd&& u) noexcept { reset(u.release()); return *this; }

    int get() const noexcept { return fd_; }
    operator int() const noexcept { return fd_; }

    int release() noexcept { int fd = fd_; fd_ = -1; return fd; }
    void reset(int fd = -1) noexcept { unique_fd(fd).swap(*this); }
    void swap(unique_fd& u) noexcept { std::swap(fd_, u.fd_); }

    unique_fd(const unique_fd&) = delete;
    unique_fd& operator=(const unique_fd&) = delete;

    // in the global namespace to override ::close(int)
    friend int close(unique_fd& u) noexcept { int closed = ::close(u.fd_); u.fd_ = -1; return closed; }

private:
    int fd_ = -1;
};
How to use a file pointer of a file descriptor with multiple child processes without getting "Bad file descriptor&q

How to use a file pointer of a file descriptor with multiple child processes without getting "Bad file descriptor&q


By : יריב טל
Date : March 29 2020, 07:55 AM
Hope this helps With the code present, the default: case of the switch, inside the double-nested for loops, contains close(pipefd[1]);. Clearly, this only works on the first iteration of the inner loop; thereafter, the pipe is broken. Subsequent children do not get an operational pipe because their parent closed the file descriptor.
The fix is to ensure that the parent does not close the write end of the pipe until all the children are created.
Why closing an Input Stream closes the associated File Descriptor as well, even the File Descriptor is shared among mult

Why closing an Input Stream closes the associated File Descriptor as well, even the File Descriptor is shared among mult


By : Tanmoy
Date : March 29 2020, 07:55 AM
hope this fix your issue If you open FileInputStreams from a File object instead of a FileDescriptor you'll get the behavior you expect:
code :
File f = new File( ... );
InputStream in1 = new FileInputStream( f );
InputStream in2 = new FileInputStream( f );
Related Posts Related Posts :
  • reinterpret_cast to a variable-sized array
  • Reading from text file to structure
  • c++ variant No such file or directory
  • What is the deduced type of a constexpr?
  • Cannot cout string when boost's io service is running
  • Case-insensitive keywords with boost::spirit::lex
  • why can't i pass two different comparators to one template function?
  • why auto doesn't detect reference type of function
  • If edges are not inserted in the deque in sorted order of weights, does 0-1 BFS produce the right answer?
  • Statically linked application - size reduction
  • C++ If-else statement using strings and comparing strings
  • Why cannot I delete unique_ptr<char[]> with reset()?
  • How to declare IN, OPTIONAL parameter along with OUT, RETVAL parameter in IDL
  • Symbol does not show up on ArcGIS Map using Qt
  • gdb stl functions still show as inlined after disabling optimizations
  • Get time difference from specific day
  • Undefined reference when using callbacks with ARM mbed
  • Is unevaluated division by 0 undefined behavior?
  • struct pointer function points to other function of other struct
  • How to print element of the map inside the vector?
  • Size of parameter pack in template specialization
  • std::string cannot be printed in gdb when it's global variable, why?
  • SONARQUBE C++ community plugin 9.0 and lower unavailable
  • How to initialize a class type used as a function parameter
  • Access object field in a CALLBACK function (WINAPI - C++)
  • c++ regex get folder from a file path
  • cannot convert argument of incomplete type 'void *' to 'const bitData'
  • C++ get/set not getting value in switch case
  • Shared pointer [] operator and ++ operator
  • standard way to put static objects together
  • Delete Elements from Vector Inside Loop
  • c++: How to flip the binary values of each bit in int
  • Strange Parent ID
  • How to fail static assertion if a class doesn't have default visibility
  • error in sscanf while scanning char[] and int
  • C++ Interfacing queue or priority_queue as template parameter of a class
  • JUCE - Member Function Not Viable: 'this' Argument Has Type const
  • Saving a brute force result for future use?
  • which header file contains ThrowIfFailed() in DirectX 12
  • 2D array pass in a function without Define (M x N) globally
  • Converting string to TDateTime in C++Builder 2009
  • Excel C API: Is there an Excel v4.0 macro function to test if a sheet is hidden?
  • std::setw for the whole operator<< of user-defined type
  • How do I setup an ODBC connection to perform multiple querys(SQLExecDirect) in c++?
  • Vectorization of modulo multiplication
  • C++ 11 thread class member function in for loop gives segmentation fault
  • Mesh boolean operation with CGAL
  • Best way to fix type conversion warnings in c++
  • How do to have a template deduce its parameters
  • Passing auto as a parameter (for pointer to object)
  • C++: MFC Application on both Windows 10 x64 & Windows 7 x64
  • Read stdin with C++ and concatenate inside string
  • No longer unable to retrieve data from QIODevice after calling readAll(). Buffer flushed?
  • g++ command line macro define byte stream
  • Why won't my variadic macro accept no arguments correctly?
  • Clang adds noinline attribute to all functions when emitting LLVM IR
  • as rptr->num=30 is same as (*rptr).num=30 but*((*rptr).ptr).name does not work why?
  • How to use get() multiple times in c++ 11 future or how to get a vector values?
  • libstdc++ cannot find thread file on Mac
  • Check if macro is odd in c++
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com