#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; }
Closed due to the fact that the essence of the question is not clear to the participants Sergey , Timofei Bondarev , Nicolas Chabanovsky ♦ 3 May '15 at 5:38 .
Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .
- Do not tell what is wrong? - Vladimir Gordeev
- 3I just adore such questions: "Why it does not work" Immediately the hand reaches for the dusty telepathic helmet ... - Alex Kapustin
- Or to minus. - Vladimir Gordeev
2 answers
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 the following access modes:O_RDONLY
,O_WRONLY
, orO_RDWR
. Write, read, write, write, write, write.
I have an example with gcc 4.5.0 (mingw) working with O_CREAT
| O_WRONLY
. With one O_CREAT file was created of zero length, respectively.
Is it not so?
сhar buf[11] = {0,1,2,3,4,5,6,7,8,9}
- No difference. - gecube
- oneNo, not without a difference. :) strlen and {0, give an interesting effect. - alexlz
- Naturally, it was implied, provided that strlen was replaced with a buffer size. But the global example will not change. - gecube