Home > Back-end >  write() writes null characters in a file instead of my string
write() writes null characters in a file instead of my string

Time:12-25

When I run my program:

char stringnums[(NUMSIZE   1) * NUMINROW   1];

    for(int i = 0; i < NUMINROW; i  )
        sprintf(stringnums, "%d %s", rand() % (NUMSIZE * 10), stringnums);
    
    if (write(desc, stringnums, strlen(stringnums)) == -1)
        perror("write");

I can see some rubbish in the end of a file:

21 21 21 27 22 22 12 12 12 12 ... strange symbols...

Full code:

#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>

#define NUMINROW 10
#define NUMSIZE 3

int main(){
    int desc;
    struct flock fl;
    char stringnums[(NUMSIZE   1) * NUMINROW   1];
    char* path = "randnums.txt";
    srand(time(NULL));

    desc = open(path, O_WRONLY);
    if (desc == -1)
        perror("open");

    if (fcntl(desc, F_GETLK, &fl) == -1)
        perror("fcntl_getlk");

    fl.l_type = F_WRLCK;
    if (fcntl(desc, F_SETLK, &fl) == -1)
        perror("fcntl_setlk");

    for(int i = 0; i < NUMINROW; i  )
        sprintf(stringnums, "%d %s", rand() % (NUMSIZE * 10), stringnums);
    
    if (write(desc, stringnums, strlen(stringnums)) == -1)
        perror("write");  

    fl.l_type = F_UNLCK;
    if (fcntl(desc, F_SETLK, &fl) == -1)
        perror("fcntl_setlk");

    if (close(desc) == -1)
        perror("close");

    return 0;
}

I have tried either to initialize stringnums as "\0" and put in write() sizeof() instead of strlen(), but it's not worked.

CodePudding user response:

As @AndreasWenzel points out, you are attempting to concatenate a string overtop of itself... This is likely to break.

The receiving buffer must be large enough to hold what you want to put into it. Don't scrimp! Why not use 10Kb instead of trying to work with what you hope to be the minimum required?

To correctly concatenate into a large enough buffer:

char buf[ 10 * 1024 ], *at = buf;
for( i = 0; i < 5; i   )
    at  = sprintf( at, "Something %d ", i );

The current length of the growng buffer can be quickly determined by at - buf.

You may also want to use snprintf() to pre-calculate the intending addition to the growing string to avoid writing beyond the end of the buffer.

  • Related