0

I need to implement a C function

ssize_t readString(int filedes, char* buf, ssize_t max);

that reads a string from file associated with file descriptor 'filedes' , into buffer 'buf' and returns the number of bytes read. The 'max' variable isn't necessary.

In other words I want to use

readString(fileDescriptor, buf);

the same way I would use

fscanf(inputFile, "%s", buf);

Below I refer what I have done so far, but it doesn't work well at all times. Do you have any suggestions for my code? Can you suggest a better implementation of this function?

Thanks

ssize_t readString(int filedes, char* buf){
    char *temp;
    int n = sizeof(buf)/sizeof(char); int i;
    ssize_t rbytes = 0; /* bytes read */
    size_t cbyte = sizeof(char);

    /* check if file is empty */
    if (read(filedes, temp, cbyte) < 1)
        return 0;

    /* eat spaces */
    while ( (*temp == ' ') || (*temp == '\n') || (*temp == '\t') )
        if (read(filedes, temp, cbyte) < 1)
            return 0;

    /* read string */
    for (i=0; i<n-1; i++){
        buf[i] = *temp;
        rbytes++;

        /* check if file is over */
        if (read(filedes, temp, cbyte) < 1)
            return rbytes;
        /* check if string is over */
        if ( (*temp == ' ') || (*temp == '\n') || (*temp == '\t') )
            break;
    }

    buf[++i] = '\0';
    return rbytes;
}
Da Mike
  • 447
  • 4
  • 17
  • 1
    "It doesn't work well at times" is not a very good problem description. – David Schwartz Nov 21 '15 at 21:00
  • Yes, give some sample input for which your program does not work, describe what the expected behaviour is (which you have done) and what the actual behaviour is. – kaylum Nov 21 '15 at 21:02

1 Answers1

1
ssize_t readString(int filedes, char* buf){
    char *temp;
    int n = sizeof(buf)/sizeof(char); int i;

I think you misunderstand what sizeof does. It figures out the size of the thing you ask it to figure out the size of. In this case, that's buf, which is a char *. So you're basically asking it how many bytes a pointer to a character takes. Presumably, you want the size of the buffer. So your function needs that as an additional parameter.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Hmm... Yes but this ---> char mess[] = "hello"; printf("sizeof(mess) = %d\n", sizeof(mess)); <--- prints "sizeof(mess) = 6" – Da Mike Nov 21 '15 at 21:23
  • 2
    @DaMike Right, because there, `mess` is an array. In your code, `buf` is a pointer to a character that happens to point to the first character in a large buffer, but `sizeof` doesn't care what a pointer points to, that doesn't change the size *of the pointer*. Of course, `sizeof (*buf)` won't work either, because buf points to *a character*, so that will return 1. Your code needs the size of the buffer, and you can't get that from just a pointer to its first character. – David Schwartz Nov 21 '15 at 21:36
  • Hmm I ran a couple of tests and you're right about the 'mess' example. And i thought I knew quite well how pointers work :D. Thanks for helping me sort this out. I'll follow your advice regarding 'readString'. Unfortunately I cant quite describe to you the cases in which it doesn't work well, but you already helped me enough. Thanks :) – Da Mike Nov 21 '15 at 21:49