Unable to print an int to a file

advertisements

I'm trying to print an int array on a file but it is giving me an error.

write_error:Bad address.

My int array is something like :

1 2 3 4 5 6 7 8 9

And I want to print it on a file as follows :

1 2 3
4 5 6
7 8 9

This is the code I'm using :

void printToFile(int listBoard[]) {
  int file_write;
  char buffer[100];
  char buffer2[10];
  if ((file_write = open("./board.txt", O_WRONLY | O_CREAT | O_TRUNC, 0700)) < 0) {
    err_sys("error output file");
  }
  if (write(file_write, snprintf(buffer2, 10, "%d",listBoard[2]), 18) != 18){
    err_sys("write_error");
  }
  if (close(file_write) != 0){
    err_sys("error close write");
  }
}

I also tried (char) but it converts the int to the ascii char.

Could you tell me what I'm missing?

Edit

My file output is :

[email protected]\00\00펈㰙\00ጠ

Code edited :

void printToFile(int listBoard[]){

  int file_write;
  char buffer[100];
  int value;
  int cont = 0;
  char buffer2[10];
  if ((file_write = open("./board.txt", O_WRONLY | O_CREAT | O_TRUNC, 0700)) < 0) {
      err_sys("error output file");
  }
  int len = snprintf(buffer2, 10, "%d", listBoard[2]);
  if (write(file_write, buffer2, len) != len) {
      err_sys("write_error");
  }
  if (close(file_write) != 0) {
      err_sys("error close write");
  }

}

Output file :

3

Last edit

This is how I did now to print it as the output that I want

if ((file_write = open("./board.txt", O_WRONLY | O_CREAT | O_TRUNC, 0700)) < 0) {
    err_sys("error output file");
}
for (int index = 0; index < SIZE_BOARD; index++) {
    if (cont == 3) {
        int len = snprintf(buffer2, 1, "%d \n", ' ');
        if(write(file_write, buffer2, len) != len) {
            err_sys("write_error");
            cont=0;
        }
    }
    int len = snprintf(buffer2, 10, "%d ", listBoard[index]);
    if (write(file_write, buffer2, len) != len) {
        err_sys("write_error");
        cont++;
    }
}

if(close(file_write) != 0) {
    err_sys("error close write");
}

But it is not doing the \n correctly. What I'm missing?


The definition of write is as follows:

ssize_t write(int fd, const void *buf, size_t count);

The definition of snprintf is as follows:

int snprintf(char *str, size_t size, const char *format, ...);

Now you're feeding snprintf to where write is expecting void *buf. i.e. the address to where the data is.

So you'll have to do

snprintf(buffer2, 10, "%d", listBoard[2]);

then

write(file_write, buffer2, 18);

EDIT:

That will fix the syntax error. Now semantically you can see that you're writing 10 bytes to buffer2 but you're writing 18 to the file. This will fill the file with junk. So i suggest.

int len = snprintf(buffer2, 10, "%d", listBoard[2]);
write(file_write, buffer2, len);

EDIT 2:

int len = snprintf(buffer2, 1, "%d \n", ' ');

First off your format specifier has 3 bytes while you're only writing 1 byte to the buffer2. Second, why is your argument ' ' when you're supposed to give the listBoard[n]?