-
write into file
I have problems writing into a file.
example: I have a file name abc.txt and i want to write/append a character to the end of the file. However, it is written to the next line.
before write:
abc
after write:
abc
H
what i want:
abcH
here is my code:
Code:
int main ( int argc, char ** argv ) {
char *fileName = argv[1];
int fd;
fd = open(fileName, O_RDWR ,0666);
lseek(fd,0,SEEK_END);
char test[1];
test[0] = 'H';
write(fd,&test,sizeof(test));
return 0;
}
Something wrong with my code?
Thanks in advance!
-
Well what you get really depends on what is at the end of the file.
To do what you ask, you would need to seek to the end, then step back a character or two, then write your data AND a replacement newline.
-
Have you verified that there is not a newline at the end of the file? If you created it with a text editor, there probably is one. If there's a newline, the new character will be written after it.
Incidentally, there is no need to provide a mode to open() if you're not passing O_CREAT.
-