Thread: if i wanna use open, read in a c++ style program, how could I do

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    17

    if i wanna use open, read in a c++ style program, how could I do

    Hello,
    I am trying to make it run, but it looks like that the usage is not correct. How could I get it. Because I also want the program running in Linux.

    Code:
    #include <iostream>
    
    // For open()
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    
    int main(void)
    {
    	int fd;
    	char * buf = NULL;
    	const char pathname[] = "\\\\.\\pipe\\com_1";
    	std::cout<<pathname<<std::endl;
    
    	fd = open(pathname, O_CREAT | O_RDWR);
    	if(fd == 0)
    		std::cout<<" open pipe failure"<<std::endl;
    	else
    	{
    		while(1)
    		{
    			read(fd, buf, sizeof(char));
    			std::cout<<*buf;
    		}
    	}
    	close(fd);
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    17
    got it
    #ifdef WIN32
    #include <io.h>
    #endif

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    What about the name of the pipe?
    Linux won't like that backslash thing you have
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    char * buf = NULL;
    
    ...
    
    read(fd, buf, sizeof(char));
    You're trying to read into an invalid address.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open a file, read it ... and ... read it again
    By Tiago in forum C Programming
    Replies: 1
    Last Post: 04-17-2010, 03:32 AM
  2. How to access excel sheet with C program(open,read,write)
    By jaininaveen in forum C Programming
    Replies: 4
    Last Post: 01-29-2006, 11:17 AM
  3. i wanna program but i dunno where to start!
    By hugh_gabble26 in forum C++ Programming
    Replies: 12
    Last Post: 10-21-2002, 06:17 PM
  4. wanna read some code
    By snakattak in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 06-19-2002, 04:21 PM