int main(int argc, char *argv[]) { FILE *fp = fopen("a.txt", "wt"); fprintf(fp, "AAAA");
result: No data has written to a.txt
I expected that everything will be fine. Because the system will close the file descriptor, then the file system driver will clear the unrelated data in its Close method. But this is not so. I tested this code on EXT4, ubuntu 11.10
Question: I thought that all file systems should hide non-solid data during its closed processing.
Does Posix have no rule?
PS This code worked well (well cleaned) on NTFS, Win7
int _tmain(int argc, _TCHAR* argv[]) { HANDLE h = CreateFile(L"D:\\a.txt", GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_ALWAYS, 0, 0); BYTE a[3]; memset(a, 'A', 3); DWORD dw; WriteFile(h, (PVOID)a, 3, &dw, 0); TerminateProcess(GetCurrentProcess(), 1); return 0; }
Edit:
I checked it again with the write system call. And he blushed well.
int main(int argc, char** argv) { int fd = open("a.txt", O_CREAT|O_TRUNC|O_WRONLY); char buf[3]; memset(buf, 'A', 3); size_t result = write(fd, buf, 3); raise(SIGTERM); exit(EXIT_SUCCESS); return 0; }