unexpected values received when writing and reading from file

In the code below i try to write and read from a file, but i get unexpected results, like after writing i cannot open the file, and when reading the file the value entered earlier is not shown

bool creat_fragments(int nFragment)
{
  int  fd, rand_value;
  char frag_path[20], buf[2];

   for(int i = 1; i <= nFragment; i++)
   {
      snprintf(frag_path, sizeof(frag_path), "fragment%d", i);
      rand_value = rand() % _RAND_RANGE + 1;
      printf("%d\n", rand_value);
       if( (fd = open(frag_path, O_RDWR | O_CREAT | O_TRUNC, FILE_MODE) ) < 0 ){
           perror("file creation failed..\n");
             return false;
   }
       snprintf(buf, sizeof(buf), "%d", rand_value);
       if(write(fd, buf, 2) < 0){
                  perror("cannot write to fragments..\n");
                     return false;}

   close(fd);

  }//for     

  char buf1[2];
  int fd1 = open("./fragmen7", O_RDONLY);
   read(fd1, buf1, 2);
    printf("value: %d\n", buf1);

   return true;
}

One main issue is you write ascii numbers but read them back as binary data.

how can i correct it?, even when i write values to the file, i cannot open it manually, it says it not readable
this time when wanna start reading, the open() returns -1

   for(int i = 1; i <= nFragment; i++)
   {
      snprintf(frag_path, sizeof(frag_path), "fragment%d.dat", i);
      rand_value = rand() % _RAND_RANGE + 1;
      printf("%d\n", rand_value);
       if( (fd = open(frag_path, O_RDWR | O_CREAT | O_TRUNC, FILE_MODE) ) < 0 ){
           perror("file creation failed..\n");
             return false;
   }
       snprintf(buf, sizeof(buf), "%d", rand_value);
       if((n = write(fd, buf, sizeof buf)) < 0){
                  perror("cannot write to fragments..\n");
                     return false;}
   close(fd);

  }//for     
/*
sleep(2);  
  char buf1[3], str[3];
  int fd1;
   if((fd1 = open("/home/user/Desktop/Link to 02-12-2010/fragmen4.dat", O_RDONLY)) < 0){
      printf("error opening file..\n");
        exit(1);
   }
   n = read(fd1, buf1, 3); 
   printf("n = %d\n", n); printf("%d", atoi(buf1));
*/

It would help if you post compilable code instead of samples.

FILE_MODE might be wrongly defined but you give no clue about its value.

What is that "Link to 02-12-2010" stuff ? What OS are you running ?

#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

my OS is Linux Ubuntu

Using the same prefix would help: fragment vs fragmen.

I am so sorry i just noticed you asked me the compilable code.

I am writing the code for a single file, i can't directly store an integer in a file as it wouldn't be readable, i have to use sprintf convert it to a string and then write it. i think the problem is that the write() operation writes the whole string
see

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<fcntl.h>
#include<ctime>
#include<cmath>
#include<iostream>
using namespace std;
#define FILE_MODE  (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
#define _PARAM 5
#define _RAND_RANGE 100
#define DOWNLOADER_FIFO "/tmp/fifo.0"

int main(int argc, char **argv){
   int fd, value = 19;
   int n;
   char c[10];

   if( (fd = open("file.txt", O_WRONLY | O_CREAT, FILE_MODE) ) < 0 ){
        perror("file creation failed..\n");
          return false;
   }      sprintf(c, "%d", value);
          if((n = write(fd, c, sizeof c)) < 0){
                  perror("\n");
                     return false;} 
   close(fd);

return 0;
}

sizeof() doesn't do what you think it does. It returns the size of a type, not the length of a string -- a string is not a type. Try strlen().

No. The files content is fine. You aren't reading it properly.

edit: sorry: the file content is bogus too. I overlook the _RAND_RANGE value. You were writing single digits in your first code.