Почему не работает этот пример?

Рейтинг: -1Ответов: 1Опубликовано: 13.05.2011
#include <string.h>
#include <fcntl.h>
#include <io.h>

int main (void)
{
    int handle;
    char buf[11] = "0123456789";

    /* создать текстовый файл из 10 байт */
    handle = open("d:\\1.txt", O_CREAT);
    write(handle, buf, strlen(buf));

    /* обрезать файл до 5 байт */
    chsize(handle, 5);
    close(handle);

    return 0;
}

Ответы

▲ 2

Name

open, creat - open and possibly create a file or device

Synopsis

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_t mode);
....
The parameter flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-only, write-only, or read/write, respectively.

У меня с gcc 4.5.0 (mingw) пример работал с O_CREAT | O_WRONLY. С одним O_CREAT файл создавался нулевой длины соответсвенно.