I am just wondering how to write a certain number of whitespace character in C. For instance, if I would like to write size whitespace character in a file descriptor, I would write something like this :
int fd = open(filename, O_RDWR|O_CREAT, 0600);
....
char* spaces = malloc(strlen(f2));
memset(spaces,' ', size);
if(write(fd, sentence1, size) =开发者_运维百科= -1){
perror("Error1 while writing in the file");
fprintf(stderr, "Error[%s, %s] : %s\n", sentence1, filename, strerror(errno));
return EXIT_FAILURE;
}
...
if(write(fd, spaces, strlen(f2)) == -1){
perror("Error2 while writing in file");
fprintf(stderr, "Error[%s]:%s\n", filename, strerror(errno));
return EXIT_FAILURE;
}
free(spaces);
....
I got an error in the write function. Can somebody tells me what is wrong with my code and how can I resolve this problem.
[EDIT] Just updated my code as @Gregg suggested, but I got this error :
*** glibc detected *** ./io2: free(): invalid next size (fast): 0x0974b008 ***
======= Backtrace: =========
/lib/i686/cmov/libc.so.6(+0x6b281)[0xb7673281]
/lib/i686/cmov/libc.so.6(+0x6cad8)[0xb7674ad8]
/lib/i686/cmov/libc.so.6(cfree+0x6d)[0xb7677bbd]
./io2[0x80489c3]
/lib/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xb761ec76]
./io2[0x8048671]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:09 842293 /home/debianbox/posix/io/io2
08049000-0804a000 rw-p 00000000 08:09 842293 /home/debianbox/posix/io/io2
0974b000-0976c000 rw-p 00000000 00:00 0 [heap]
b74e2000-b74ff000 r-xp 00000000 08:01 12068 /lib/libgcc_s.so.1
b74ff000-b7500000 rw-p 0001c000 08:01 12068 /lib/libgcc_s.so.1
b7500000-b7521000 rw-p 00000000 00:00 0
b7521000-b7600000 ---p 00000000 00:00 0
b7607000-b7608000 rw-p 00000000 00:00 0
b7608000-b7748000 r-xp 00000000 08:01 16133 /lib/i686/cmov/libc-2.11.2.so
b7748000-b774a000 r--p 0013f000 08:01 16133 /lib/i686/cmov/libc-2.11.2.so
b774a000-b774b000 rw-p 00141000 08:01 16133 /lib/i686/cmov/libc-2.11.2.so
b774b000-b774e000 rw-p 00000000 00:00 0
b775e000-b7760000 rw-p 00000000 00:00 0
b7760000-b7761000 r-xp 00000000 00:00 0 [vdso]
b7761000-b777c000 r-xp 00000000 08:01 12335 /lib/ld-2.11.2.so
b777c000-b777d000 r--p 0001a000 08:01 12335 /lib/ld-2.11.2.so
b777d000-b777e000 rw-p 0001b000 08:01 12335 /lib/ld-2.11.2.so
bfe06000-bfe1b000 rw-p 00000000 00:00 0 [stack]
Abandon
For an unknown reason, it fails at the free(spaces) line.
[EDIT] I finally correct my problem. @dmh200 suggested, i wrongly use the size variable and strlen(f2). Useless question. Thanks for your help
The second parameter of write()
must point to a buffer that contains (at least) the number of bytes specified in the size
parameter.
The constant string " "
contains two bytes only - a space and a trailing nul byte. Calling write(fd, " ", size)
with size > 2
is an error.
If you know you will always write less than say, 10 spaces, then you could:
write(fd, " ", size);
If you don't know how many spaces you need, then you could dynamically allocate a buffer, fill it with spaces, and write that:
char *spaces = malloc(size);
memset(spaces, ' ', size);
write(fd, spaces, size);
free(spaces);
精彩评论