-3

I am trying to append some text to an already opened file. I'd like to append the pid number at the end of my file. For example: Lorem ipsum dolor sit amet orci aliquam. 14872. My code works fine except I am failing at making my program print the space between the last character of the file and the first digit of the pid. I've tried to use lseek() by adding 1 as offset from the end of the file and, as I previously said, failed miseably. I don't understand what's wrong, because apparently I am getting all expected results:

Buffer size: 5.
Buffer content: 14872.
Current position: 41.

and then this output:

Lorem ipsum dolor sit amet orci aliquam.14872

which makes no sense to me. So here it is my code:

void writeintheend(const int fd, const int pid, const int base)
{
    char *buffer;
    int buffer_size = getcharsfromint(pid, base);
    printf("Buffer size: %d.\n", buffer_size);
    buffer = (char *) malloc (sizeof(char) * buffer_size);
    sprintf(buffer, "%d", pid);
    printf("Buffer content: %s.\n", buffer);

    //It should add a space and then write the pid
    lseek(fd, 1, SEEK_END);
    printf("Current position: %d.\n", lseek(fd, 0, SEEK_CUR));
    write(fd, buffer, buffer_size);
    free(buffer);
}
haunted85
  • 1,601
  • 6
  • 24
  • 40
  • 2
    Since you never check the return values of any of your function calls, it seems that you either don't really care, or might prefer a different language... – Kerrek SB May 17 '14 at 13:17
  • 2
    `lseek` won't add a space, it will leave a zero byte there. If you want to add a space, you have to write the space character. – Barmar May 17 '14 at 13:21
  • 1
    Just do `sprintf(buffer, " %d", pid);` – lethal-guitar May 17 '14 at 13:22
  • 1
    @KerrekSB it's not that I don't care, I am new to dealing with system calls and UNIX programming, I am just learning and hungry for best practices and constructive criticism. So cheers. – haunted85 May 17 '14 at 13:26
  • @Barmar thank you a lot for clearing that out, it was going to give me lots of headaches. – haunted85 May 17 '14 at 13:27
  • 1
    @haunted85: Sure, constructive criticism: read all the manuals of all the functions you use, and make sure to handle *all* possible paths. (Nobody is born an expert - the difference lies in who reads documentation! :-)) – Kerrek SB May 17 '14 at 13:28
  • @haunted85, I ran your code (gcc 4.5 on SUSE SLES 11). Being that I didn't have access to `getcharsfromint()`, I just set `buffer_size' to `4`. I ran it twice. Each time I ran it, the file size increased 5 bytes (as it should). Perhaps there is a problem in your `getcharsfromint()` function? – Mahonri Moriancumer May 17 '14 at 13:46
  • @MahonriMoriancumer no that function works as it should, lethal-guitar has actually solved my issue and Barman explained why. Thank you for your help.:) – haunted85 May 17 '14 at 13:49
  • Btw: If the text representation of `pid` is five characters and your `buffer` also is five characters, `sprintf()`ing the pid into `buffer` provokes undefined behaviour, as the `0`-terminator (which will add `sprintf()` to `buffer` to ake it a vlaid C-"string") will be printed out of `buffer`'s bounds, that is to `buffer[5]`, which is one past the end of `buffer`. – alk May 17 '14 at 15:59

1 Answers1

2

Seeking off the end of a file doesn't pad it with spaces but leaves null bytes until something is written into the gap. You'll need to write a leading ' ' to your pid buffer then write that to the end (without the seek past the end of file).

Barmar
  • 741,623
  • 53
  • 500
  • 612
Gwyn Evans
  • 1,361
  • 7
  • 17