Thread: read file descriptor function in c

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Post read file descriptor function in c

    Hi all,

    I have problem with read() function. It doesn't read at all. Always return -1. my code is like below:
    Code:
    void myRead()
    {
    	int fd,ret;
    	char buf[BUFSIZ]; 
    
    	fd = open("sam.txt" , O_WRONLY); 	
    	if (fd == -1)
    	{
    		printf("Error in openning the file!\n");
    		exit(0);
    	}
    	//read the file
    	ret = read(fd,buf,BUFSIZ);  
    	if (ret == -1)
    	{
    		printf("Error in reading!\n");
    		exit(0);
    	}
    	close(fd);
    }
    I am sure file can be open, but once start to read it always return -1.

    Please suggest or comment. Thanks a lot.


    --Mario.


    Code Tags added by Kermi3

  2. #2
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.

    Good Luck,
    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  3. #3
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    What's the error message you are getting? Include the following line if you don't know how to get the error message:
    Code:
    perror(" read: ");

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    25
    You're opening the file for writing, not reading...change your call to open to
    Code:
    fd = open ("sam.txt", O_RDONLY);
    . if you on a *nix system, type man 2 open at the command line, and you will see all of the options for file status and mode for the open system call...

    -[E]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM