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 , write return
  • read , write don't return int, ssize_t
  • open(path, 0) works, should use o_rdonly
  • you using void main instead of correct int main
  • as jonathan leffler pointed out, should use buffer instead of &buffer

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -