c - Accidently creating gigantic file with write -


this code using:

 #include <stdio.h>  #include <stdlib.h>  void main(){   test = creat("test",0751);   close(test);   test = open("test",2);   write(test, "123456789101112131415",21);   lseek(test,-2,2);   read(test,swap_array,2);   write(test,swap_array,2);   lseek(test,-6, 1);   write(test,"xx",2);  } 

this creates 8gb file containing instead of inserting "xx" in between numbers intend. wrong code have it?

you have failed include appropriate header files. should include, @ minimum:

#include <sys/types.h> #include <unistd.h> #include <fcntl.h> 

additionally, give access symbolic constants required make code maintainable.

here working version of program:

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h>   int main() {  char swap_array[2];  int test = creat("test",0751);  close(test);  test = open("test",o_rdwr);  write(test, "123456789101112131415",21);  lseek(test,-2,seek_end);  read(test,swap_array,2);  write(test,swap_array,2);  lseek(test,-6, seek_cur);  write(test,"xx",2);  return 0; } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -