C programming: write() integers to a text file

Keywords: c linux system-calls


I m trying to write a couple of integers on a text file using write() sys call, but insead of numbers i got strange symbols (00\00\00\00\00\00).

Gedit is telling me that there is an issue with encoding and that the file is not in UTF-8. I m compiling with gcc which should has the UTF-8 codec set by default. I solved the '\00' by changing the sizeof(i) with 1 but i cant resolve the numbers.

int main(int argc, char const *argv[]) {
   int fd=open("test.txt", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
   int i = 2;
   write(fd, &i, sizeof(i)); }