system call
Please tell me what is wrong with this code... There are no errors, it creates the file, but it does not write to the file:
#include<stdio.h> #include<fcntl.h> main() { char *p = ("hello world"); int fp; fp = open ("samplex.c", O_CREAT, 0666); write (fp,p,11); fork (); } |
check out man 2 open
Quote:
Code:
int main(void) |
probably the lack of a close(). The string "Hello World." is probably short enough that the OS cached the write, but then you never properly closed the string, so the cache may not ever be written to disk. And, out of curiosity, what is the purpose of the fork()?
Regarding bartonski's comment: Umm, oh yeah, that too. |
Quote:
Whenever you have *anything* that can be opened and closed, eg. open and closed braces, malloc/free, fopen/fclose, open and close perens... you get the picture... write both at the same time, then write your code between them. This is guaranteed to save you hours of debugging. Caveat to the above: If you have some sort of branch make sure that any resources that you have open get closed regardless of how the conditional turns out. |
All times are GMT -5. The time now is 06:13 PM. |