(यह "HelloWorld" लिखने के लिए एक "HelloWorld" फ़ाइल में, और फिर पढ़ा पाठ चाहिए था) निम्नलिखित कोड को देखते हुए:सी प्रणाली खुला कॉल/पठन/लेखन/बंद और O_CREAT | O_EXCL
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define FNAME "helloworld"
int main(){
int filedes, nbytes;
char buf[128];
/* Creates a file */
if((filedes=open(FNAME, O_CREAT | O_EXCL | O_WRONLY | O_APPEND,
S_IRUSR | S_IWUSR)) == -1){
write(2, "Error1\n", 7);
}
/* Writes hello world to file */
if(write(filedes, FNAME, 10) != 10)
write(2, "Error2\n", 7);
/* Close file */
close(filedes);
if((filedes = open(FNAME, O_RDONLY))==-1)
write(2, "Error3\n", 7);
/* Prints file contents on screen */
if((nbytes=read(filedes, buf, 128)) == -1)
write(2, "Error4\n", 7);
if(write(1, buf, nbytes) != nbytes)
write(2, "Error5\n", 7);
/* Close file after read */
close(filedes);
return (0);
}
पहली बार मैं कार्यक्रम चलाने के लिए, उत्पादन होता है: हर बार जब मैं कार्यक्रम चलाने के लिए
helloworld
उसके बाद, उत्पादन होता है:
Error1
Error2
helloworld
मुझे समझ में नहीं आता कि टेक्स्ट क्यों शामिल नहीं है, क्योंकि मैंने O_APPEND फ़ाइल निर्दिष्ट की है। ऐसा इसलिए है क्योंकि मैंने O_CREAT को शामिल किया है? यह फ़ाइल पहले ही बनाई गई है, क्या O_CREAT को अनदेखा नहीं किया जाना चाहिए?
धन्यवाद, O_EXCL को हटाकर कोड कार्यात्मक है। क्या आप कृपया मुझे O_EXCL का उपयोग समझा सकते हैं। –