Porting ofstream attach() in linux gcc


 
Thread Tools Search this Thread
Top Forums Programming Porting ofstream attach() in linux gcc
# 1  
Old 01-04-2011
Porting ofstream attach() in linux gcc

Hi

We have a huge codebase in HP-UX and we are porting them in RH-Linux. I am facing the problem of making the following code work in gcc -

Code:
ofstream ofs;
int fd = open(fileName, openState, openMode));
func(fd);
......
......
const Boolean func(const int fileDescriptor)
{
    ofs.attach(fileDescriptor);
    if (! ofs)
    {
        closeIt();
        ofs.attach(fileDescriptor);
        if (! ofs)
            return(FAIL);
    }
    return(SUCCESS);
}

Here one file is opened and then the ofs variable is attached to that file descriptor.
Unlike HP-UX compiler, gcc doesn't have the attach() function. So how I can make the code compatible? I tried to solve the problem in various ways like how it is described here - Gerhard Wesp - Re: Attaching cout || cerr to an ostream or here - [c++] How to create a std:Smiliefstream to a temp file? - Stack Overflow. But nothing is helping this particular issue since fd is a filedescriptor and not a filebuf. Can you please help me out?

Thanks
# 2  
Old 01-04-2011
There's no standard, portable way to do this. Like ghostbusters, you're not supposed to cross the streams. Smilie But g++ iostreams do have some extensions to make it possible. Adapting an example found here I tried this:

Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <ext/stdio_filebuf.h>  // __gnu_cxx::stdio_filebuf

using namespace std;

int main(void)
{
    std::string str;
    int fd=open("whatever", O_RDONLY); // Get an FD
    FILE *fp=fdopen(fd, "r"); // convert it into a FILE *
    // create a file buffer(NOT an iostream yet) from FILE *
    __gnu_cxx::stdio_filebuf<char> fb (fp, ios_base::in);
    istream fs (&fb);    // create a stream from file buffer

    fs >> str;
    cout << str << endl;
    return(0);
}

Testing this on a file containing 'asdf' it does print asdf, so while a bit tortuous it does actually work. It being that tortuous also makes it plain that you are using an extension, which is probably the point...

I don't know if the stdio_filebuf closes the FILE * for you when it goes out of scope or not.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-13-2011
Hi !!!

First of all thanks a ton to your post - appreciate your time and effort to help me out! I took long time to reply here because of some other urgent work.

I took your help and wrote the following program which creates an ostream object from the file descriptor -

Code:
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ext/stdio_filebuf.h>  // __gnu_cxx::stdio_filebuf

int main()
{
int fd;
FILE *fp;
if (-1 == (fd=open("/tmp/test.txt", (O_WRONLY|O_CREAT|O_APPEND), 0666)))
{
    cout<<"Unable to open file"<<endl;
    exit(1);
}
cout << "File Descriptor # is: " << fd << endl;
if (NULL == (fp = fdopen(fd, "a")))
{
    cout<<"fdopen failed"<<endl;
    close(fd);
    exit(1);
}

// create a file buffer(NOT an iostream yet) from FILE *
__gnu_cxx::stdio_filebuf<char, std::char_traits<char> > fb (fp, std::ios_base::out);

ostream os(&fb);
os << "Hello World!" << endl;
close(fd); //ostream doesn't have the close() function
return 0;
}

To create the ofstream object in the same way we have to do the following -
Code:
#include <fstream.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ext/stdio_filebuf.h>  // __gnu_cxx::stdio_filebuf

int main()
{
int fd;
FILE *fp;
std::ofstream ofs;

if (-1 == (fd=open("/tmp/test.txt", (O_WRONLY|O_CREAT|O_APPEND), 0666)))
{
    cout<<"Unable to open file"<<endl;
    exit(1);
}
cout << "File Descriptor # is: " << fd << endl;
if (NULL == (fp = fdopen(fd, "a")))
{
    cout<<"fdopen failed"<<endl;
    close(fd);
    exit(1);
}

// create a file buffer(NOT an iostream yet) from FILE *
__gnu_cxx::stdio_filebuf<char> fb(fp, std::ios::out);

ofs.std::ios::rdbuf(&fb);
ofs << "Hello World!" << endl;
ofs.close();
return 0;
}

Both the programs and running fine and will be a ready references for other users facing the same problem.

Once again thanking you for your generous support!

Last edited by nsinha; 01-17-2011 at 02:14 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

attach is not a member of ofstream

Hi, Code written in C++ got compiled successfully using Sun 4.2 Compiler on Solaris 6. As part of migration, i am using same code and trying to compile using Sun 5.8 C++ compiler(Sun Studio11) on Solaris 10 and could not compile the below line, outStr.attach(1); // here outStr is declared... (1 Reply)
Discussion started by: shafi2all
1 Replies

2. Shell Programming and Scripting

Porting from Solaris to Linux

Can any one please help the use of "cu command in Solaris" and as well as in Linux :confused: (1 Reply)
Discussion started by: sabee.prakash
1 Replies

3. Solaris

Porting a Linux Driver to Solaris

Hi all, Has anyone experience with proting a Linux driver (C-code) to Solaris 10? I have a Sunix SATA card with a inicio1622 chipset, but no driver available. From the website of inicio I downloaded the drivercode for Linux 2.4. Having done some investigation I found a Solaris driver... (4 Replies)
Discussion started by: longwave
4 Replies

4. Filesystems, Disks and Memory

Porting OSE to Linux

Hi, I was trying to port efs_mount(OSE system call) to a LInux.The efs_mount function is used to mount a volume on the indicated device dev.Upon successful completion of this OSE sytem call a volume manager (VM) will be available through which files on this volume are accessed. The Syntax for... (4 Replies)
Discussion started by: roshantraj30
4 Replies

5. Programming

Nucleus to Linux porting

I am new to Linux programming and my work involves changing an abstraction layer which made Nucleus calls, to Linux calls. In Case of Events Nucleus has calls like NU_Set_Events() NU_Retrieve_Events() Can I use the POSIX thread conditional variables for Linux? Can I use the System V calls... (1 Reply)
Discussion started by: taklubaba
1 Replies

6. IP Networking

Porting DHCP from Linux to VxWorks

DHCP Porting (2 Replies)
Discussion started by: Sunny Shivam
2 Replies

7. Linux

Porting DHCP from Linux to VxWorks

Hello All, I have a code of DHCP which is implemented on Linux. During porting this code from Linux to VxWorks, I come up with following errors:- jects\freedom\ap\udhcp\socket.c C:\projects\freedom\ap\udhcp\socket.c: In function `read_interface': C:\projects\freedom\ap\udhcp\socket.c:79:... (1 Reply)
Discussion started by: Sunny Shivam
1 Replies

8. Linux

when porting from HP-UX to Linux

helo, i m porting HP-UX socket application to Linux SSL-socket application. I have use htonl() in HP-UX. so when i use it in Linux, data transf is not done and application become soem time crashed. now when i remove htonl() in linux, then i got data but it will not proper order or some data may... (1 Reply)
Discussion started by: amitpansuria
1 Replies

9. Programming

Porting From Linux To Hpux

Gents, i'm a senior applications developer and need to port a Linux server application ( no additional / special libraries or unique header files ) to a HPUX enviroment. Any chance to compile it on the Linux using flags to create an HPUX binary with gcc? (8 Replies)
Discussion started by: anak0nda
8 Replies

10. UNIX for Dummies Questions & Answers

HP-UX to linux porting

Hi all, i wanted to port some HP-UX code to linux. can anybody point to some documents or resources that would help me in doing the porting.. thanks in advance Arun Prakash (0 Replies)
Discussion started by: arunprakash
0 Replies
Login or Register to Ask a Question