c - Reading/Writing with System Calls -
i'm trying write code (in c) read 1 file, , write created file. part i'm struggling while loop, supposed continuously read , write until end of file. first 120 characters, write them, write 'xyz' file, when try read/write again, ^@ , bunch of garbage. don't want solution on how this, tell me doing wrong/forgot do.
#include <sys/types.h> #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdlib.h> void main() { int a, b, c; int xyz = creat("xyz.doc", 777); if(xyz<0) { printf("error creat"); exit(0); } int xxxx = open("/usr/class/cis660/xx.xx", 0); if(xxxx<0) { printf("error open"); exit(0); } int tryread = 1; int trywrite; while (tryread > 0) { char buffer[120]; tryread = read(xxxx, &buffer, 120); trywrite = write(xyz, &buffer, 120); char xyz[3] = "xyz"; trywrite = write(xyz, &xyz, 120); } }
char xyz[3] = "xyz"; trywrite = write(xyz, &xyz, 120);
this main problem. trying write 120, when have 3. have other problems:
- you not checking
read,writereturn read,writedon't returnint,ssize_topen(path, 0)works, should useo_rdonly- you using
void maininstead of correctint main - as jonathan leffler pointed out, should use
bufferinstead of&buffer
Comments
Post a Comment