I am trying to write into a file using the write()
function (included in <unistd.h>
). The program is simple: when running the executable, I type a message and then, the message and my user id (Linux UID) are saved into the file.
$ ./notetaker "Hello"
I was expecting that the following value could be saved into the file:
1000
Hello
There are two problems:
- my file is being written in hexadecimal (when I open it using Sublime Text, all I can see are hexadecimal values)
- the integer inserted representing my user id is not correct
This is the result that I'm getting when running cat notes.txt
:
�
Hello
When I open the notes.txt file with Sublime Text, I can read the following data:
e803 0000 0a48 656c 6c6f 0a
The first 4 bytes are not equal to "1000".
Why is my file being saved with hexadecimal values? And why is the number incorrect?
This is my source code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
void write_uid(const int);
void write_note(const int, void *);
int main(int argc, char *argv[])
{
char *buffer = (char *) malloc(100);
if(buffer == NULL) exit(0);
strcpy(buffer, argv[1]);
char *filename = (char *) malloc(50);
if(filename == NULL) exit(0);
strcpy(filename, "notes.txt");
int file_descriptor = open(filename, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR);
if(file_descriptor == -1) exit(0);
write_uid(file_descriptor);
write_note(file_descriptor, buffer);
if(close(file_descriptor) == -1) exit(0);
printf("Your note has been saved successfully. \n");
free(buffer);
free(filename);
return 1;
}
void write_uid(const int file_descriptor)
{
int current_user_id = getuid();
int uid_write_result_code = write(file_descriptor, ¤t_user_id, sizeof(current_user_id));
int new_line_write_result_code = write(file_descriptor, "\n", 1);
if(uid_write_result_code < 0 || new_line_write_result_code < 0)
exit(0);
}
void write_note(const int file_descriptor, void *buffer)
{
int note_write_result_code = write(file_descriptor, buffer, strlen( (char *) buffer ));
int new_line_write_result_code = write(file_descriptor, "\n", 1);
if(note_write_result_code < 0 || new_line_write_result_code < 0)
exit(0);
}
I'm using an Ubuntu 14.04 Trusty Tahr (x64), and my GCC version is 4.8.4. Also, when compiling using the -Wall option, no warning is shown.