c - Read() is reading junk data before reading actual data -
im having issue using read() function in c.
i have file lets filex has contents:
data outputted
however when open file , use read on garbage data using code below
ssize_t reader = 0; ssize_t writer = 0; char buffer[256]; reader = read(myfile, buffer, 256); //check if reader -1, if exit(1) writer = write(1, buffer, 256); //check if writer -1, if exit(1)
the read function seems run twice. once bunch of garbage data followed actual data in file. idea how remedy this?
are sure buffer filled '\0', finished '\0' ? , call write 256 - in 3rd arg
#include <fcntl.h> int main() { const int max_size = 256; char buffer[max_size] = {}; int my_input = open("input", o_text, s_iread); ssize_t reader; ssize_t writer; reader = read(my_input, buffer, max_size); if(reader != -1) writer = write(1, buffer, reader); return 0; }
Comments
Post a Comment