C भाषा open फंक्शन पूर्ण गाइड | प्रयोग, फ्ल्याग, त्रुटि समाधान

目次

1. C gengo को open कार्य के हो?

1.1 open कार्यको भूमिका

C gengo को open कार्य फाइल खोल्न वा सिर्जना गर्नको लागि प्रणाली कल हो।
सामान्यतया, Linux र UNIX प्रकारका OS को वातावरणमा प्रयोग गरिन्छ, र मानक पुस्तकालय कार्य fopen भन्दा कम स्तरको फाइल अपरेसन गर्दा प्रयोग गरिन्छ।

1.2 fopen कार्यसँगको भिन्नता

C gengo मा, fopen नामको फाइल खोल्नको लागि मानक पुस्तकालय कार्य पनि छ, तर open कार्यसँग यसको प्रयोग फरक हुन्छ।

functionFeatures
openLow-level API, returns a file descriptor, system call
fopenHigh-level API, returns , supports buffering

open कार्यको प्रयोग

  • लॉग फाइलको अभिलेखO_APPEND प्रयोग गरेर)
  • अस्थायी फाइलहरूको सिर्जनाO_TMPFILE)
  • असिंक्रोनस प्रोसेसिंग (O_NONBLOCK)
  • विशिष्ट पहुँच अधिकार भएको फाइलको सिर्जनाO_CREAT + mode)

यसरी, open कार्य साधारण फाइल अपरेसनदेखि उन्नत फाइल व्यवस्थापनसम्म विस्तृत प्रयोगमा प्रयोग गरिन्छ।

2. open कार्यको मूलभूत प्रयोग【नमूना कोड सहित】

2.1 open कार्यको प्रोटोटाइप

open कार्य, fcntl.h हेडर फाइलमा परिभाषित गरिएको छ, र तल जस्तै लेखिन्छ।

#include 
int open(const char *path, int flags, mode_t mode);
  • path:खोलिने फाइलको पथ(उदाहरण: "sample.txt")。
  • flagsफाइल खोल्ने मोड र व्यवहार निर्दिष्ट गर्ने फ्ल्यागहरू (उदाहरण: O_RDONLY)。
  • modeफाइल सिर्जना गर्दा अनुमति(O_CREAT निर्दिष्ट गर्दा आवश्यक)।
  • रिटर्न मान:
  • सफलता समय: फाइल डिस्क्रिप्टर (0 उपरोक्त पूर्णांकहरू)
  • विफलता हुँदा:-1 लाई फिर्ता गर्‍यो,errno मा त्रुटि सामग्री भण्डारण गरिन्छ।

2.2 open कार्यको मूलभूत प्रयोग

तलको उदाहरणले फाइललाई पढ्ने‑लेख्ने मोड(O_RDWR)मा खोल्छ, र नयाँ सिर्जना गर्दा0644 अधिकारको साथ बनाउँछ।

#include 
#include 
#include 
int main() {
    int fd = open("sample.txt", O_RDWR | O_CREAT, 0644);
    if (fd == -1) {
        perror("open");
        return 1;
    }
    printf("File opened successfully with descriptor %dn", fd);
    close(fd);
    return 0;
}

2.3 open कार्यको त्रुटि ह्यान्डलिङ

open कार्य असफल भएमा, -1 फिर्ता गर्छ, र errno नामको ग्लोबल भेरिएबलमा त्रुटि कोड राखिन्छ।
perror() प्रयोग गर्दा, त्रुटि विवरणलाई सजिलै देखाउन सकिन्छ।

#include 
#include 
#include 
#include 
int main() {
    int fd = open("/root/protected.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    close(fd);
    return 0;
}

त्रुटि कोडको उदाहरण:

  • EACCES(Permission denied):अधिकार अपर्याप्त
  • ENOENT(No such file or directory):निर्दिष्ट गरिएको फाइल अवस्थित छैन

2.4 open कार्यको विकल्पको उपयोग

  • केवल पढ्नको लागि खोल्नुहोस्
  int fd = open("sample.txt", O_RDONLY);
  • लेखन-केवल मोडमा खोल्नुहोस्
  int fd = open("sample.txt", O_WRONLY);
  • फाइल अवस्थित नभएमा नयाँ सिर्जना गर्नुहोस्
  int fd = open("sample.txt", O_WRONLY | O_CREAT, 0644);
  • अवस्थित फाइल खोलेर त्यसको सामग्री खाली गर्नुहोस्.
  int fd = open("sample.txt", O_WRONLY | O_TRUNC);
侍エンジニア塾

3. open कार्यको झण्डा सूची【पूर्ण मार्गदर्शिका】

3.1 मूलभूत झण्डा

3.1.1 पढ्ने/लेख्ने मोड निर्दिष्ट गर्ने झण्डा

flagExplanation
O_RDONLYOpen in read-only mode
O_WRONLYOpen in write-only mode
O_RDWROpen in read/write mode

3.1.2 प्रयोग उदाहरण

int fd1 = open("file.txt", O_RDONLY);  // पठन मात्र
int fd2 = open("file.txt", O_WRONLY);  // लेखन मात्र
int fd3 = open("file.txt", O_RDWR);    // पठन र लेखन

3.2 फाइल निर्माण‑प्रबन्धन सम्बन्धी झण्डा

3.2.1 O_CREAT (फाइल नभएमा नयाँ सिर्जना)

int fd = open("newfile.txt", O_WRONLY | O_CREAT, 0644);

3.2.2 O_EXCL (O_CREAT सँग मिलाएर, यदि फाइल पहिले नै छ भने त्रुटि)

int fd = open("newfile.txt", O_WRONLY | O_CREAT | O_EXCL, 0644);
if (fd == -1) {
    perror("File already exists");
}

3.2.3 O_TRUNC (अवस्थित फाइल खोल्दा सामग्री मेटाइन्छ)

int fd = open("log.txt", O_WRONLY | O_TRUNC);

3.3 लेखन कार्यलाई नियन्त्रण गर्ने झण्डा

3.3.1 O_APPEND (जोड मोडमा खोल्ने)

int fd = open("log.txt", O_WRONLY | O_APPEND);
write(fd, "New log entryn", 14);

3.4 असिंक्रोनस‑विशेष कार्यलाई नियन्त्रण गर्ने झण्डा

3.4.1 O_NONBLOCK (नन‑ब्लकिङ मोड)

int fd = open("fifo_pipe", O_RDONLY | O_NONBLOCK);

3.4.2 O_SYNC (समक्रमित लेखन)

int fd = open("important_data.txt", O_WRONLY | O_SYNC);

3.4.3 O_NOFOLLOW (सिम्बोलिक लिङ्कलाई ट्र्याक नगर्ने)

int fd = open("symlink_file", O_RDONLY | O_NOFOLLOW);

3.5 झण्डाको संयोजन

PurposeCombination of flags
Read/Write Enabled + New CreationO_RDWR | O_CREAT, 0644
write-only + append modeO_WRONLY | O_APPEND
Read-only + Asynchronous processingO_RDONLY | O_NONBLOCK
Open a file and clear its contentsO_WRONLY | O_TRUNC
Create new only if no existing fileO_WRONLY | O_CREAT | O_EXCL, 0644

4. mode(फाइल अनुमति)को विवरण

4.1 mode के हो?

mode भनेको, open फलन प्रयोग गरेर नयाँ फाइल सिर्जना गर्दा(O_CREAT फ्ल्याग प्रयोग गरेर)、 फाइलको पहुँच अधिकार निर्दिष्ट गर्नको लागि मान हो।

int fd = open("newfile.txt", O_WRONLY | O_CREAT, 0644);

4.2 mode को आधारभूत सेटिङ

Permission valueAccess rightsmeaning
0---Access Denied
1--xRun only
2-w-Write-only
4r--Read-only
6rw-Read & Write
7rwxread, write, execute

4.3 modeumask को सम्बन्ध

UNIX系OS मा, mode को निर्दिष्ट मान सिधै लागू हुँदैन, तर umask(प्रयोगकर्ता मास्क)को प्रभाव पाउँछ।

0666(निर्दिष्ट मोड)
- 0022(umask)
------------
0644(वास्तविक रूपमा लागू हुने अनुमति)

वर्तमान umask जाँच गर्नुहोस्:

$ umask
0022

4.4 chmod द्वारा अनुमति परिवर्तन

यदि सिर्जना पछि अनुमति परिवर्तन गर्न चाहनुहुन्छ भने, chmod आदेश प्रयोग गर्नुहोस्।

$ chmod 600 secret.txt  # गोप्य फाइल
$ chmod 755 script.sh   # चलाउन योग्य फाइल

C भाषा मा chmod प्रयोग गर्ने अवस्थामा:

#include 
chmod("file.txt", 0644);

4.5 mode लाई विचार गरी open फलनको व्यावहारिक उदाहरण

#include 
#include 
#include 
int main() {
    int fd = open("secure.txt", O_WRONLY | O_CREAT, 0600);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    printf("File created successfully with descriptor %dn", fd);
    close(fd);
    return 0;
}

5. open फङ्क्शन र सम्बन्धित सिस्टम कलहरू

5.1 close फङ्क्शन (फाइल बन्द गर्ने)

5.1.1 close को प्रोटोटाइप

#include 
int close(int fd);

5.1.2 close को प्रयोग उदाहरण

#include 
#include 
#include 
int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    printf("File opened successfully.n");
    if (close(fd) == -1) {
        perror("close failed");
        return 1;
    }
    printf("File closed successfully.n");
    return 0;
}

5.2 read फङ्क्शन (फाइलबाट डेटा पढ्ने)

5.2.1 read को प्रोटोटाइप

#include 
ssize_t read(int fd, void *buf, size_t count);

5.2.2 read को प्रयोग उदाहरण

#include 
#include 
#include 
int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    char buffer[128];
    ssize_t bytesRead = read(fd, buffer, sizeof(buffer) - 1);
    if (bytesRead == -1) {
        perror("read failed");
        close(fd);
        return 1;
    }
    buffer[bytesRead] = '\0';
    printf("Read data: %sn", buffer);
    close(fd);
    return 0;
}

5.3 write फङ्क्शन (फाइलमा डेटा लेख्ने)

5.3.1 write को प्रोटोटाइप

#include 
ssize_t write(int fd, const void *buf, size_t count);

5.3.2 write को प्रयोग उदाहरण

#include 
#include 
#include 
int main() {
    int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    const char *data = "Hello, World!n";
    ssize_t bytesWritten = write(fd, data, 14);
    if (bytesWritten == -1) {
        perror("write failed");
        close(fd);
        return 1;
    }
    printf("Written %ld bytes to file.n", bytesWritten);
    close(fd);
    return 0;
}

5.4 lseek फङ्क्शन (फाइलभित्रको स्थिती परिवर्तन गर्ने)

5.4.1 lseek को प्रोटोटाइप

#include 
off_t lseek(int fd, off_t offset, int whence);

5.4.2 lseek को प्रयोग उदाहरण

#include 
#include 
#include 
int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    off_t newPos = lseek(fd, 5, SEEK_SET);
    if (newPos == -1) {
        perror("lseek failed");
        close(fd);
        return 1;
    }
    char buffer[10];
    read(fd, buffer, 5);
    buffer[5] = '\0';
    printf("Read after seek: %sn", buffer);
    close(fd);
    return 0;
}

6. openफङ्क्शनको व्यावहारिक प्रयोग उदाहरण

6.1 लग फाइललाई थप मोडमा खोल्नुहोस्

#include 
#include 
#include 
int main() {
    int fd = open("log.txt", O_WRONLY | O_CREAT | O_APPEND, 0644);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    const char *log_entry = "New log entryn";
    write(fd, log_entry, 14);
    close(fd);
    return 0;
}

6.2 अस्थायी फाइल सिर्जना गर्नुहोस्

#include 
#include 
#include 
int main() {
    int fd = open("/tmp", O_TMPFILE | O_RDWR, 0644);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    const char *data = "Temporary datan";
    write(fd, data, 15);
    printf("Temporary file created (fd = %d)n", fd);
    close(fd);
    return 0;
}

6.3 O_NONBLOCK प्रयोग गरेर असिन्क्रोनस प्रोसेसिङ

#include 
#include 
#include 
int main() {
    int fd = open("fifo_pipe", O_RDONLY | O_NONBLOCK);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    printf("File opened in non-blocking moden");
    close(fd);
    return 0;
}

6.4 त्रुटि ह्यान्डलिङ्गको कार्यान्वयन

#include 
#include 
#include 
#include 
int main() {
    int fd = open("/root/protected.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        if (errno == EACCES) {
            printf("Permission deniedn");
        } else if (errno == ENOENT) {
            printf("File does not existn");
        }
        return 1;
    }
    close(fd);
    return 0;
}

6.5 lseek प्रयोग गरेर फाइलको स्थिती परिवर्तन

#include 
#include 
#include 
int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    lseek(fd, 10, SEEK_SET);
    char buffer[11];
    read(fd, buffer, 10);
    buffer[10] = '\0';
    printf("Read after seek: %sn", buffer);
    close(fd);
    return 0;
}

7. open कार्यसम्बन्धी अक्सर सोधिने प्रश्न (FAQ)

7.1 open कार्य र fopen कार्यको भिन्नता के हो?

FunctionFeaturesUsage
opensystem call, returns a file descriptor (integer)Low-level file operations, system programming
fopenStandard C library, returnsGeneral file I/O using buffering

7.2 open कार्यमा mode अनिवार्य रूपमा निर्दिष्ट गर्नुपर्छ?

  • O_CREATउपयोग गर्दा मात्र आवश्यक
int fd = open("newfile.txt", O_WRONLY | O_CREAT, 0644);

7.3 O_APPEND निर्दिष्ट गर्दा, लेख्ने स्थान सधैं अन्त्यमा हुन्छ?

  • हो,O_APPEND यदि निर्दिष्ट गरियो भने लेखन सधैं अन्त्यमा गरिन्छ।
int fd = open("log.txt", O_WRONLY | O_APPEND);
write(fd, "New entryn", 10);
close(fd);

7.4 O_NONBLOCK फ्ल्याग कहिले प्रयोग गर्ने?

  • When you want immediate return with FIFO (pipe) or socket
int fd = open("fifo_pipe", O_RDONLY | O_NONBLOCK);
if (fd == -1) {
    perror("open failed");
}

7.5 open कार्यले सिम्बोलिक लिङ्क खोल्दा के हुन्छ?

  • सामान्यतया, लिंकको फाइल खोल्छ, तरO_NOFOLLOWयदि प्रयोग गर्नुहुन्छ भने रोक्न सकिन्छ
int fd = open("symlink.txt", O_RDONLY | O_NOFOLLOW);
if (fd == -1) {
    perror("open failed");
}

7.6 open कार्यले फाइल साइज कसरी प्राप्त गर्ने?

#include 
#include 
#include 
int main() {
    int fd = open("sample.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        return 1;
    }
    off_t filesize = lseek(fd, 0, SEEK_END);
    printf("File size: %ld bytesn", filesize);
    close(fd);
    return 0;
}

7.7 open कार्य असफल हुने मुख्य कारणहरू के हुन्?

Error codemeaning
EACCESNo access rights
ENOENTFile does not exist
EEXISTO_CREAT | O_EXCL
EMFILEExceeded the upper limit on the number of openable files for the process
ENOSPCInsufficient disk space
#include 
#include 
#include 
#include 
int main() {
    int fd = open("/root/protected.txt", O_RDONLY);
    if (fd == -1) {
        perror("open failed");
        if (errno == EACCES) {
            printf("Permission deniedn");
        } else if (errno == ENOENT) {
            printf("File does not existn");
        }
        return 1;
    }
    close(fd);
    return 0;
}

8. सारांश

8.1 openकार्यको आधारभूत

  • openयो फङ्सन, फाइल खोल्न/सिर्जना गर्नको लागि प्रणाली कल हो।
  • fopenयसको विपरीत,फाइल डिस्क्रिप्टर (FD) फिर्ता गर्ने
  • close(fd) उपयुक्त रूपमा कार्यान्वयन गरेर, स्रोत रिसाव रोक्नुहोस्।

8.2 openकार्यका मुख्य फ्ल्याग

flagExplanation
O_RDONLYOpen in read-only mode
O_WRONLYOpen in write-only mode
O_RDWROpen in read-write mode
O_CREATCreate a new file if it does not exist.
O_TRUNCDelete the contents of existing files
O_APPENDAlways append at the end when writing
O_NONBLOCKnon-blocking mode
O_NOFOLLOWDo not open symbolic links

8.3 openकार्य र सम्बन्धित सिस्टम कल

  • close(fd):फाइल डिस्क्रिप्टरलाई मुक्त गर्नुहोस्।
  • read(fd, buf, count):फाइलबाट डेटा पढ्नुहोस्।
  • write(fd, buf, count)फाइलमा डेटा लेख्नुहोस्।
  • lseek(fd, offset, whence):फाइलको पढ्ने/लेख्ने स्थान सार्नुहोस्।

8.4 openकार्यको व्यावहारिक प्रयोग उदाहरण

  • लॉग फाइलमा थप्नेO_APPEND लाई प्रयोग गरेर अन्त्यमा थप्नुहोस्。
  • अस्थायी फाइलको सिर्जनाO_TMPFILE लाई उपयोग गर्नुहोस्।
  • असिंक्रोनस प्रोसेसिंगO_NONBLOCK ब्लोकिङ्गलाई टाढा राख्नुहोस्।

8.5 openकार्यका सावधानीहरू

  • उपयुक्त रूपमाclose(fd) गर्ने कामसंसाधन रिसाव रोक्नको लागि।
  • त्रुटि प्रबन्धन गर्नेerrno पुष्टि गरेर उपयुक्त कार्यवाही गर्नुहोस्।
  • अनुमति सेटिङहरूमा त्रुटि नगर्नुहोस्mode निर्देशनमा त्रुटि भएमा, सुरक्षा जोखिम हुन्छ।

8.6 यस लेखको सारांश

SectionContent
openSystem calls for low-level file operations
openO_CREATmode
openO_RDONLY, O_WRONLY, O_RDWR, O_CREAT, etc.
Related system callsclose, read, write, lseek
Practical usage examplesLogging, temporary files, asynchronous processing, error handling
FAQfopenO_APPEND

8.7 अन्तमा

C भाषा को openकार्य हो, सिस्टम स्तरमा फाइल सञ्चालनमा आवश्यक अनिवार्य सिस्टम कल हो। उचित रूपमा प्रयोग गर्दा, प्रभावकारी फाइल व्यवस्थापन र असिन्क्रोनस प्रोसेसिङ प्राप्त गर्न सकिन्छ। यस लेखमा सिकेको ज्ञानलाई प्रयोग गरी, सुरक्षित र बलियो प्रोग्राम विकास गर्नुहोस्।

侍エンジニア塾