read() help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dave13
    New Member
    • May 2009
    • 2

    read() help

    Hi there

    Below is a small bit of test code for a project im working on, eventually the programs going to be a simple file transfer system, reading a file and then sending it out of a socket, but for now I cant get any files to open. I've been pouring over it for hours and I can't find up whats going wrong, the files there and has data in, but all the read() will return is -1 with a stderr message of Bad File Descriptor, but i just cant figure it out.

    Any help would be massively appreciated

    Thank you

    Dave

    #include <stdio.h>
    #include <sys/socket.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <string.h>

    #define RCVBUFSIZE 2048

    int main(int argc, char** argv)
    {
    FILE *iput_ptr;
    char rcvBuffer[RCVBUFSIZE];
    char fileName[8];
    int nRead;

    strcpy(fileName , "test.txt") ;
    memset(rcvBuffe r,0,RCVBUFSIZE) ; // Zero out Receive Buffer
    printf("%s", fileName);
    iput_ptr = fopen(fileName, "r");
    if(iput_ptr != NULL)
    {
    do
    {
    printf("pre read");
    nRead = read(iput_ptr, rcvBuffer, RCVBUFSIZE-1);
    printf("%i", nRead);

    }
    while (nRead > 0);
    }
    return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Your code works for me. I just used an fread() instead of your read().

    I did have to make some changes, however. Your fileName array is 8 but "test.txt" is 9. It's the old null terminator again.

    Comment

    • Dave13
      New Member
      • May 2009
      • 2

      #3
      Converted it to fread() and its working a treat!! Thank you!!

      Comment

      Working...