Thread: read() returns invalid file descriptor ???

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    3

    Unhappy read() returns invalid file descriptor ???

    Code:
    FILE *mysql;
    mysql = fopen("mysql.txt","rw");
    
    char buffer[1000];
    
    if (read(mysql,buffer,sizeof(buffer)) > 0) {
           printf ("%s\n",buffer);
    }
    how can this be explained? what am i doing wrong?
    read(134520840, 0xbfbea748, 1000) = -1 EBADF (Bad file descriptor)

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You need to use fread() instead. To be short, file I/O functions come in groups. read() works best if you used open() to get a file descriptor from it. You used fopen(), which is fine.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    read() is not a standard function in the C/C++ library.

    It is (IIRC) a windows and unix specific function. It does not accept an argument that is a pointer to a FILE: the argument is typically an integer which is a file descriptor returned by a function named open().

  4. #4
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    You should try to use something like this:
    Code:
    fread (buffer, sizeof buffer, 1, mysql);
    if you want to read file line by line then I suggest using fgets function!
    Last edited by Micko; 08-04-2006 at 06:49 AM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > mysql = fopen("mysql.txt","rw");
    Read the manual again, "rw" isn't a valid mode.
    Also look up the perror() function which will print a wordy description of why various things fail.

    Also as noted, you're using read() where you should be using fread()
    (Or fopen() where you should be using open())
    Either way, make them match up with one another.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM