Altera_Forum
Honored Contributor
16 years agofail to create output file - simple io problem
Any idea what is wrong with the code below? The code failed to create the sample.txt file. Runtime error.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int main(void){ const void *str = "hello\n"; size_t count; FILE *fp = open("sample.txt", O_WRONLY); printf("Opening file sample.txt\n"); if(fp == null) {printf("Open failed");return EXIT_FAILURE; } else {printf("Open succeeded\n"); } printf("Writing to file ...\n"); count = write(fp, str, strlen(str)); // x, [/INDENT]printf("Writing completed \n"); close(fp); return EXIT_SUCCESS;}