C File reading doesn't stop -


i writing simple server allows sending files using http protocol. have function puts file buffer. goes before read. file size printed correctly. on read program waits.

char *get_file(char *dir) {     fprintf(stderr, "get file\n");     char *buff;     int fd;     if (fd = open(dir, o_rdonly) == -1) {         fprintf(stderr, "no such file: %s\n", dir);         exit(6);     }      size_t size = fsize(dir);      fprintf(stderr, "opened file, size: %ld\n", size);     buff = malloc(size);     read(fd, buff, size);      fprintf(stderr, "to downloaded: %s\n", buff);     char *response = make_file_response(buff);     return response; } 

you have issue statement

if (fd = open(dir, o_rdonly) == -1)  

according operator precendence == evaluated first , thus, fd being assigned value of comparison , not opened file descriptor.

with compiler warnings enabled parentheses suggested, , correted expression be

if ((fd = open(dir, o_rdonly)) == -1)  /*  ^~~~~~~~~~~~~~~~~~~~~~~~~^ */ 

would first assign return value of open() fd , comparison performed.

if print value of fd see it's 0 if open() succeeded i.e. returned value not -1 , 1 otherwise.


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -