- 1 1. परिचय
- 2 2. read कार्यको मूलभूत
- 3 3. readफङ्क्शनको प्रयोग उदाहरण
- 4 4. read कार्यको प्रयोग
- 5 5. readfunction प्रयोग गर्दा ध्यान दिनुपर्ने बुँदाहरू
- 6 6. बारम्बार सोधिने प्रश्न (FAQ)
- 6.1 Q1. read कार्य र fread कार्यको भिन्नता के हो?
- 6.2 Q2. read कार्यले 0 फिर्ता गर्दा, त्यो त्रुटि हो?
- 6.3 Q3. गैर-रोक मोडमा read कार्य प्रयोग गर्ने तरिका के हो?
- 6.4 Q4. read कार्यले -1 फिर्ता गर्दा, के गर्नु पर्छ?
- 6.5 Q5. फाइलको आकार धेरै ठूलो भएमा, कसरी प्रक्रिया गर्ने?
- 6.6 Q6. read कार्यमा डेटा बीचमा किन टुट्छ?
- 6.7 FAQ सारांश
- 7 7. सारांश
1. परिचय
C भाषा कोread
फङ्सनले, सिस्टम प्रोग्रामिङमा मूलभूत कार्यहरूमध्ये एक हो। फाइल वा डिभाइसबाट डेटा सिधै पढ्नको लागि कम स्तरको इनपुट/आउटपुट फङ्सन हो, र अन्य इनपुट/आउटपुट फङ्सनहरूसँग तुलना गर्दा सिस्टमको व्यवहारलाई विस्तृत रूपमा नियन्त्रण गर्न सक्ने विशेषता छ।
यस लेखमा,read
फङ्सनको मूलभूत प्रयोगदेखि अनुप्रयोग, सामान्य प्रश्नहरूको समाधानसम्म व्यापक रूपमा व्याख्या गर्नेछौं। विशेष गरी, शुरुआतीहरूले सजिलै अड्किने बुँदाहरू र व्यावहारिक कोड उदाहरणहरूमा केन्द्रित गरी व्याख्या गर्नेछौं, र मध्यवर्ती स्तरका लागि असिंक्रोनस I/O र त्रुटि ह्यान्डलिङ्गको गहिरो विश्लेषण पनि गर्नेछौं। यो लेख पढेपछि,read
फङ्सनलाई प्रभावकारी र सुरक्षित रूपमा प्रयोग गर्न आवश्यक ज्ञान प्राप्त हुनेछ।
C भाषा कोread
फङ्सन भने के हो?
read
फङ्सन POSIX मानकमा परिभाषित सिस्टम कल मध्ये एक हो, र Linux तथा UNIX प्रकारका OS मा व्यापक रूपमा प्रयोग हुन्छ। यो फङ्सन फाइल डिस्क्रिप्टर मार्फत डेटा पढ्छ। उदाहरणका लागि, फाइल, मानक इनपुट, सॉकेट आदि विभिन्न डेटा स्रोतहरूबाट पढ्नलाई समर्थन गर्दछ।
read
फङ्सन कम स्तरको अपरेसन सम्भव बनाउँछ, तर शुरुआतीहरूका लागि यसलाई ह्यान्डल गर्न कठिन भागहरू पनि छन्। विशेष गरी, बफर व्यवस्थापन र त्रुटि ह्यान्डलिङ्गको समझ आवश्यक छ। अन्य उच्च स्तरका फङ्सनहरू (जस्तै: fread
, scanf
) सँग तुलना गर्दा, read
फङ्सन OS मा प्रत्यक्ष निर्भरता भएको व्यवहार राख्दछ, जसले अधिक लचिलो नियन्त्रण सम्भव बनाउँछ, तर त्यसको लागि सावधानीपूर्वक कार्यान्वयन आवश्यक पर्छ।
अन्य इनपुट/आउटपुट फङ्सनहरूसँगको भिन्नता
C भाषामा,read
फङ्सन बाहेक पनि इनपुट/आउटपुटलाई ह्यान्डल गर्ने केही फङ्सनहरू छन्। प्रत्येकको विशेषताहरूलाई सरल रूपमा तुलना गरौं।
Function name | Level | Primary uses | Features |
---|---|---|---|
read | Low-level | Reading from files and devices | System calls、high flexibility |
fread | High level | Reading file stream | Standard C library、easy to use |
scanf | High level | Reading from standard input | Format specification is possible |
read
फङ्सन कम स्तरको अपरेसन आवश्यक पर्ने परिस्थितिहरू (जस्तै: डिभाइस सञ्चार वा ठूलो आकारको फाइल प्रोसेसिङ) मा विशेष उपयोगी हुन्छ। अर्कोतर्फ, fread
र scanf
सजिलो र सरलता आवश्यक पर्ने परिस्थितिहरूमा उपयुक्त छन्।
लेखमा समेटिने सामग्री
यस लेखमा, तलका विषयहरूलाई विस्तृत रूपमा व्याख्या गर्नेछौं।
- Basic usage
read
- विशिष्ट प्रयोग उदाहरण फाइल पढ्ने, मानक इनपुट, सॉकेट संचार जस्ता व्यावहारिक उदाहरणहरू देखाउँछौं।
- अनुप्रयोग उदाहरणहरू र समस्या समाधान हामी असिंक्रोनस I/O को सेटअप गर्ने तरिका र त्रुटि प्रबन्धनका लागि उत्कृष्ट अभ्यासहरू व्याख्या गर्नेछौं।
- सधैँ सोधिने प्रश्नहरू र समाधानहरू पाठकहरूले सहजै उठाउने प्रश्नहरूलाई FAQ ढाँचामा समेट्छौं।
शुरुआतीदेखि मध्यवर्ती स्तरसम्म, विस्तृत पाठक वर्गलाई समेट्ने सामग्री प्रदान गर्ने लक्ष्य राख्छौं।
2. read
कार्यको मूलभूत
C Gengo को read
कार्य फाइल वा उपकरणबाट डेटा पढ्नको लागि कम स्तरको I/O कार्य हो। यस खण्डमा, read
कार्यको मूलभूत विशिष्टता बारे, विशिष्ट कोड उदाहरणहरू सहित व्याख्या गरिन्छ।
read
कार्यको प्रोटोटाइप
read
कार्यको प्रोटोटाइप तलको जस्तै छ:
ssize_t read(int fd, void *buf, size_t count);
आर्गुमेन्टको व्याख्या
fd
(फाइल डिस्क्रिप्टर)
- Specify the target to read.
- For example, you can specify file descriptors obtained from the function, such as the standard input () and standard output (), among others.
buf
(buffer)
- Pass the address of the memory area where data is temporarily stored.
- This area must be allocated with sufficient size in advance to hold the data that has been read.
count
(बाइट संख्या)
- Specify the maximum number of bytes to read.
- It is recommended to be below the buffer size.
फिर्ती मान
- Normal:
- In case of error: returns -1 and checks the error content with .
मूलभूत प्रयोग उदाहरण
तल फाइलबाट डेटा पढ्ने मूलभूत उदाहरण हो।
कोड उदाहरण
#include
#include
#include
int main() {
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open file");
return 1;
}
char buffer[128];
ssize_t bytesRead;
while ((bytesRead = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytesRead] = ' '; // पढिएको डेटा स्ट्रिङको रूपमा ह्यान्डल गर्न टर्मिनेटर सेट गर्नुहोस्
printf("%s", buffer); // पढिएको डेटा आउटपुट गर्नुहोस्
}
if (bytesRead == -1) {
perror("Failed to read file");
}
close(fd);
return 0;
}
कोड व्याख्या
open
फङ्शन प्रयोग गरेर फाइल खोल्नुहोस्
O_RDONLY
- If the file cannot be opened, an error is output.
read
फङ्शन प्रयोग गरेर डेटा पढ्नुहोस्
- Read up to 128 bytes from the file into .
- If the read is successful, the actual number of bytes read is returned.
- त्रुटि प्रबन्धन
- If the file does not exist, or if there is no read permission, it returns .
- बफरको अन्त्य सेट गर्नुहोस्
- To output the read data as a string, add at the end.
read
कार्य प्रयोग गर्दा ध्यान दिनुपर्ने बुँदाहरू
बफर आकार र सुरक्षा
- If you try to read data that exceeds the buffer size, memory corruption may occur. Please specify a value less than or equal to the buffer size for .
EOF (फाइल अन्त्य) को ह्यान्डलिंग
read
0
आंशिक पढाइ
read
read
3. read
फङ्क्शनको प्रयोग उदाहरण
यस खण्डमा, read
फङ्क्शनलाई वास्तविक रूपमा प्रयोग गर्ने केही उदाहरणहरू प्रस्तुत गरिन्छ। आधारभूत फाइल पढाइदेखि, मानक इनपुट, र नेटवर्क सॉकेट संचारमा प्रयोगका उदाहरणहरू सम्म विस्तृत रूपमा व्याख्या गरिन्छ।
आधारभूत फाइल पढाइ
पहिले, फाइलबाट डेटा पढ्ने आधारभूत प्रयोग तरिका प्रस्तुत गरिन्छ। read
फङ्क्शनलाई टेक्स्ट फाइल वा बाइनरी फाइलको पढाइमा पनि प्रयोग गर्न सकिन्छ।
कोड उदाहरण: टेक्स्ट फाइलको पढाइ
#include
#include
#include
int main() {
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open file");
return 1;
}
char buffer[128];
ssize_t bytesRead;
while ((bytesRead = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytesRead] = ' '; // पठन गरिएको डेटा स्ट्रिङको रूपमा ह्यान्डल गर्न टर्मिनेटर सेट गर्नुहोस्
printf("%s", buffer); // पठन गरिएको डेटा आउटपुट गर्नुहोस्
}
if (bytesRead == -1) {
perror("Failed to read file");
}
close(fd);
return 0;
}
कोड व्याख्या
- फाइल खोल्नुहोस्
open
read
फङ्सनमा लूप प्रोसेसिङ
- Read data repeatedly into the buffer until reaching the end of file (EOF).
- त्रुटि प्रबंधन
read
-1
perror
- फाइल बन्द गर्नुहोस्
- Finally, the function closes the file descriptor and releases resources.
मानक इनपुटबाट डेटा प्राप्ति
अर्को, मानक इनपुट(किबोर्ड इनपुट)बाट डेटा प्राप्त गर्ने उदाहरण देखाइन्छ। यो सरल CLI उपकरण वा अन्तरक्रियात्मक प्रोग्राममा प्रायः प्रयोग हुने विधि हो।
कोड उदाहरण: प्रयोगकर्ता इनपुटको प्राप्ति
#include
#include
int main() {
char buffer[64];
printf("कुनै पाठ प्रविष्ट गर्नुहोस्: ");
ssize_t bytesRead = read(0, buffer, sizeof(buffer) - 1); // मानक इनपुट निर्दिष्ट (0 भनेको stdin)
if (bytesRead == -1) {
perror("Failed to read input");
return 1;
}
buffer[bytesRead] = ' '; // Null समाप्ति वर्ण थप्नुहोस्
printf("तपाईंले प्रविष्ट गर्नुभयो: %s\n", buffer);
return 0;
}
कोड व्याख्या
- मानक इनपुट निर्दिष्ट गर्नुहोस्
read
0
- बफरको अन्त्य सेटिङ
- To allow the read data to be treated as a string, we add to the end.
- त्रुटि प्रबंधन
- If reading input fails, use to display the error details.
सॉकेट संचारमा डेटा प्राप्ति
read
फङ्क्शन नेटवर्क प्रोग्रामिङमा पनि प्रयोग हुन्छ। यहाँ, सरल सर्भर प्रोग्रामलाई उदाहरणको रूपमा, सॉकेटबाट डेटा प्राप्त गर्ने तरिका व्याख्या गरिन्छ।
कोड उदाहरण: सॉकेटबाट डेटा प्राप्ति
तलको कोड क्लाइन्टबाट सन्देश प्राप्त गरी प्रदर्शन गर्ने सर्भर प्रोग्रामको उदाहरण हो।
#include
#include
#include
#include
#include
int main() {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("Socket creation failed");
return 1;
}
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) == -1) {
perror("Bind failed");
close(server_fd);
return 1;
}
if (listen(server_fd, 3) == -1) {
perror("Listen failed");
close(server_fd);
return 1;
}
int client_fd = accept(server_fd, NULL, NULL);
if (client_fd == -1) {
perror("Accept failed");
close(server_fd);
return 1;
}
char buffer[1024];
ssize_t bytesRead = read(client_fd, buffer, sizeof(buffer) - 1);
if (bytesRead > 0) {
buffer[bytesRead] = ' ';
printf("Message received: %s
", buffer);
} else if (bytesRead == -1) {
perror("Read failed");
}
close(client_fd);
close(server_fd);
return 0;
}
कोड व्याख्या
- सॉकेटको सिर्जना
socket
- address binding
- Specify the server’s IP address and port, and bind the socket.
- जडानको प्रतिक्षा
listen
- क्लाइन्ट कनेक्शन स्वीकार
accept
client_fd
- डेटा पढ्ने
- Data sent from the client is read by the function and stored in the buffer.
प्रयोग उदाहरणहरूको सारांश
यी उदाहरणहरूले read
फङ्क्शन केवल फाइल अपरेसनमा मात्र सीमित नभई विभिन्न परिस्थितिहरूमा प्रयोग गर्न सकिन्छ भन्ने देखाउँछन्। विशेष गरी, सॉकेट संचारमा, read
फङ्क्शन डेटा प्राप्तिमा महत्वपूर्ण भूमिका खेल्छ।
4. read
कार्यको प्रयोग
read
कार्यले केवल आधारभूत फाइल अपरेसन मात्र होइन, उन्नत प्रोग्रामिङमा पनि प्रयोग गर्न सकिन्छ। यस खण्डमा, असिन्क्रोनस I/O, ठूलो डेटा को प्रभावकारी प्रक्रिया, बाइनरी डेटा को पढाइ जस्ता प्रयोगका उदाहरणहरू बारे व्याख्या गरिन्छ।
असिन्क्रोनस I/O मा प्रयोग
असिन्क्रोनस I/O प्रयोग गर्दा, read
कार्यले डेटा पर्खँदा अन्य कार्यहरू चलाउन सकिन्छ। यसले अनुप्रयोगको प्रदर्शन सुधार्छ।
असिन्क्रोनस मोडको सेटिङ
असिन्क्रोनस मोड सक्षम गर्न, fcntl
कार्य प्रयोग गरेर फाइल डिस्क्रिप्टरलाई नन-ब्लकिङ मोडमा सेट गर्नुहोस्।
कोड उदाहरण: असिन्क्रोनस I/O को सेटिङ
#include
#include
#include
#include
int main() {
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open file");
return 1;
}
// गैर-रोक मोड सेट गर्नुहोस्
int flags = fcntl(fd, F_GETFL, 0);
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
perror("Failed to set non-blocking mode");
close(fd);
return 1;
}
char buffer[128];
ssize_t bytesRead;
while ((bytesRead = read(fd, buffer, sizeof(buffer) - 1)) != 0) {
if (bytesRead > 0) {
buffer[bytesRead] = ' ';
printf("Data read: %s\n", buffer);
} else if (bytesRead == -1 && errno == EAGAIN) {
printf("No data available, try again later\n");
} else if (bytesRead == -1) {
perror("Read error");
break;
}
}
close(fd);
return 0;
}
कोड व्याख्या
- गैर-ब्लकिङ मोड सेटिङ
fcntl
O_NONBLOCK
- त्रुटि प्रबंधन
- If the data is not yet available, is set to or .
- लूपमा पढाइ
- In asynchronous I/O, it is important to design to repeatedly call as needed.
ठूलो डेटा प्रभावकारी रूपमा लोड गर्ने तरिका
ठूलो डेटा प्रक्रिया गर्दा, प्रभावकारी मेमोरी व्यवस्थापन र बफर सेटिङ महत्त्वपूर्ण हुन्छ। तल, प्रभावकारी डेटा पढाइ हासिल गर्ने प्रविधिहरू व्याख्या गरिन्छ।
प्रविधि1: बफर साइजको अनुकूलन
- Increasing the buffer size reduces the number of system calls and improves performance.
- Generally, it is considered good to match the system’s page size (which can be obtained with ).
कोड उदाहरण: ठूलो बफर प्रयोग
#include
#include
#include
#include
int main() {
int fd = open("largefile.bin", O_RDONLY);
if (fd == -1) {
perror("Failed to open file");
return 1;
}
size_t bufferSize = 4096; // 4KB
char *buffer = malloc(bufferSize);
if (!buffer) {
perror("Failed to allocate buffer");
close(fd);
return 1;
}
ssize_t bytesRead;
while ((bytesRead = read(fd, buffer, bufferSize)) > 0) {
printf("Read %zd bytes
", bytesRead);
// आवश्यकताको अनुसार प्रक्रिया थप्नुहोस्
}
if (bytesRead == -1) {
perror("Read error");
}
free(buffer);
close(fd);
return 0;
}
बाइनरी डेटा को पढाइ
read
कार्यले पाठ्य डेटा मात्र होइन, छवि फाइल वा कार्यान्वयन फाइल जस्ता बाइनरी डेटा को पढाइमा पनि उपयुक्त छ। बाइनरी डेटा ह्यान्डल गर्दा, डेटा को एन्डियन र स्ट्रक्चरको अलाइनमेन्टमा ध्यान दिनु आवश्यक छ।
कोड उदाहरण: बाइनरी फाइलको पढाइ
#include
#include
#include
#include
typedef struct {
uint32_t id;
float value;
} DataRecord;
int main() {
int fd = open("data.bin", O_RDONLY);
if (fd == -1) {
perror("Failed to open file");
return 1;
}
DataRecord record;
ssize_t bytesRead;
while ((bytesRead = read(fd, &record, sizeof(record))) > 0) {
printf("ID: %u, Value: %.2f
", record.id, record.value);
}
if (bytesRead == -1) {
perror("Read error");
}
close(fd);
return 0;
}
कोड व्याख्या
- संरचना लोड गर्ने
read
- डेटा प्रक्रिया
- You can process data by directly referencing struct members.
- एन्डियनको विचार
- If a file is generated on a different platform, endian conversion may be required.
प्रयोगका उदाहरणहरूको सारांश
read
कार्यको प्रयोगले उन्नत प्रोग्रामिङ कार्यहरूलाई प्रभावकारी रूपमा प्रक्रिया गर्न सम्भव बनाउँछ। असिन्क्रोनस I/O प्रयोग गर्दा प्रणाली स्रोतहरूलाई प्रभावकारी रूपमा प्रयोग गर्न सकिन्छ, ठूलो डेटा र बाइनरी फाइलको पढाइलाई पनि लचिलो रूपमा सामना गर्न सकिन्छ।
5. read
function प्रयोग गर्दा ध्यान दिनुपर्ने बुँदाहरू
read
function लचिलो र शक्तिशाली उपकरण हो, तर प्रयोग गर्दा ध्यान दिनुपर्ने बुँदाहरू केही छन्। यस भागमा, read
function लाई सुरक्षित र प्रभावकारी रूपमा प्रयोग गर्नका लागि महत्वपूर्ण ध्यान दिनुपर्ने बुँदाहरू व्याख्या गरिन्छ।
बफर ओभरफ्लो रोकथाम
read
function प्रयोग गर्दा, निर्दिष्ट बफर आकारभन्दा बढी डेटा पढ्दा, मेमोरी क्षति(बफर ओभरफ्लो)हुन सक्छ। यसले प्रोग्रामको क्र्यास वा सुरक्षा कमजोरी उत्पन्न गर्न सक्छ।
उपायहरू
- उपयुक्त बफर साइज सेटिङ
- The buffer size ensures sufficient capacity beyond the expected data size.
read
count
- बफरको अन्त्य सेटिङ
- If you are not handling binary data, you must always add (Null character) after the data you read, and treat it as a string.
कोड उदाहरण: सुरक्षित बफर व्यवस्थापन
#include
#include
#include
int main() {
char buffer[128];
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open file");
return 1;
}
ssize_t bytesRead = read(fd, buffer, sizeof(buffer) - 1);
if (bytesRead == -1) {
perror("Failed to read file");
close(fd);
return 1;
}
buffer[bytesRead] = ' '; // बफरको अन्त्य सेट गर्नुहोस्
printf("Data read: %s
", buffer);
close(fd);
return 0;
}
EOF(फाइल अन्त्य) को प्रक्रिया विधि
read
function ले 0 फिर्ता गरेमा, त्यो EOF(End of File: फाइल अन्त्य) हो। यस अवस्थामा, डेटा पढाइ समाप्त भएको हो। EOF को ह्यान्डलिङ्ग गलत गरेमा, अनन्त लूप वा अनावश्यक प्रक्रिया उत्पन्न हुन सक्छ।
सही EOF पत्ता लगाउने
- रिटर्न मान जाँच गर्नुहोस्
read
0
- लूपमा सर्त निर्दिष्ट गर्ने
- To correctly handle EOF, use the loop condition .
कोड उदाहरण: EOF को सही प्रक्रिया
#include
#include
#include
int main() {
char buffer[128];
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open file");
return 1;
}
ssize_t bytesRead;
while ((bytesRead = read(fd, buffer, sizeof(buffer) - 1)) > 0) {
buffer[bytesRead] = ' ';
printf("Data read: %s
", buffer);
}
if (bytesRead == -1) {
perror("Error while reading file");
}
close(fd);
return 0;
}
आंशिक पढाइको समस्या समाधान
read
function ले निर्दिष्ट बाइट संख्या सबै एकै पटक पढ्न सक्दैन। विशेष गरी सॉकेट वा पाइपमा प्रयोग गर्दा, आंशिक डेटा मात्र पढ्न सकिन्छ। यो व्यवहार डेटा आगमनको समय वा प्रणालीको स्थितिले प्रभावित हुन्छ।
कारण4>
- सिग्नलको प्रभाव
- When a system call is interrupted by a signal, the function may stop in the middle.
- गैर‑ब्लकिङ मोड
- In non-blocking mode, it returns immediately if there is insufficient available data.
- बफर आकारको अभाव
- If there is data that does not fit in the buffer during a single call, multiple reads are required.
समाधान विधि
- पुनः प्रयास प्रक्रिया
- If a partial read occurs, implement a loop to read the remaining data again.
- त्रुटि कोड जाँच गर्नुहोस्
- Use to add handling for specific error codes such as and .
कोड उदाहरण: आंशिक पढाइको समाधान
#include
#include
#include
#include
int main() {
char buffer[128];
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("Failed to open file");
return 1;
}
ssize_t bytesRead;
size_t totalBytesRead = 0;
while ((bytesRead = read(fd, buffer + totalBytesRead, sizeof(buffer) - totalBytesRead - 1)) > 0) {
totalBytesRead += bytesRead;
}
if (bytesRead == -1 && errno != EINTR) {
perror("Read error");
} else {
buffer[totalBytesRead] = ' ';
printf("Total data read: %s
", buffer);
}
close(fd);
return 0;
}
प्रयोग गर्दा ध्यान दिनुपर्ने बुँदाहरूको सारांश
- buffer size
- EOF detection
- Partial read
यी बुँदाहरूलाई बुझेर अभ्यास गर्दा, read
function लाई सुरक्षित र प्रभावकारी रूपमा प्रयोग गर्न सकिन्छ।
6. बारम्बार सोधिने प्रश्न (FAQ)
यहाँ, C Gengo को read
कार्यको बारेमा पाठकहरूले अक्सर उठाउने प्रश्नहरूलाई उठाइन्छ, र तिनीहरूको समाधान र बुँदाहरूलाई व्याख्या गरिन्छ। शुरुआतीदेखि मध्यवर्ती स्तरसम्म, बुझाइलाई गहिरो बनाउने सामग्री प्रदान गरिन्छ।
Q1. read
कार्य र fread
कार्यको भिन्नता के हो?
उत्तर:
read
function:- With system calls, you directly manipulate the OS.
- We use file descriptors to perform low-level I/O.
- While it offers high flexibility, error handling and buffer management are required.
fread
function:- The standard C library functions provide high-level input and output.
- Read data from a stream using a file pointer.
- Buffer management is handled automatically, making it easy to use.
विभाजनको बिन्दु:
read
function:fread
function:
Q2. read
कार्यले 0 फिर्ता गर्दा, त्यो त्रुटि हो?
उत्तर:
होइन, read
कार्यले 0
फिर्ता गर्दा, त्यो EOF(End of File: फाइलको अन्त्य) लाई जनाउँछ। यो सामान्य कार्य हो, र फाइलभित्रको डेटा सबै पढिसकेको संकेत हो।
समाधान:
- If EOF is detected, terminate the reading process.
- When using in a loop, you can properly handle EOF by conditioning on .
Q3. गैर-रोक मोडमा read
कार्य प्रयोग गर्ने तरिका के हो?
उत्तर:
गैर-रोक मोडमा, read
कार्यले डेटा आगमनको प्रतिक्षा नगरी तुरुन्तै फिर्ता हुन्छ। यो सेटिङलाई तलको जस्तै fcntl
कार्य प्रयोग गरेर गरिन्छ।
कोड उदाहरण:
#include
#include
#include
#include
int main() {
int fd = open("example.txt", O_RDONLY);
if (fd == -1) {
perror("फाइल खोल्न असफल भयो");
return 1;
}
// गैर-रोक मोड सेट गर्दैछ
int flags = fcntl(fd, F_GETFL, 0);
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
perror("गैर-रोक मोड सेट गर्न असफल भयो");
close(fd);
return 1;
}
char buffer[128];
ssize_t bytesRead = read(fd, buffer, sizeof(buffer));
if (bytesRead == -1 && errno == EAGAIN) {
printf("अहिले डेटा उपलब्ध छैन\n");
} else if (bytesRead > 0) {
buffer[bytesRead] = ' ';
printf("पढिएको डेटा: %s\n", buffer);
}
close(fd);
return 0;
}
ध्यान दिनु पर्ने बुँदाहरू:
- If data is not available, the function returns , and is set to or .
- If you are not familiar with asynchronous processing, knowledge of polling and event-driven programming is required.
Q4. read
कार्यले -1
फिर्ता गर्दा, के गर्नु पर्छ?
उत्तर:
read
कार्यले -1
फिर्ता गर्दा, त्यो त्रुटि भएको संकेत हो। त्रुटिको विवरण ग्लोबल भेरिएबल errno
मा हेर्न सकिन्छ।
मुख्य त्रुटि कोडहरू:
EINTR
: The was interrupted by a signal. The process needs to be retried.EAGAIN
orEWOULDBLOCK
: In non-blocking mode, the data is not available.- Other errors: When the file descriptor is invalid (), etc.
समाधान उदाहरण:
if (bytesRead == -1) {
if (errno == EINTR) {
// पुनः प्रयास प्रक्रिया
} else {
perror("पढ्न असफल भयो");
}
}
Q5. फाइलको आकार धेरै ठूलो भएमा, कसरी प्रक्रिया गर्ने?
उत्तर:
read
कार्य प्रयोग गरेर ठूलो फाइल प्रक्रिया गर्दा, तलका विधिहरू विचार गर्नुहोस्।
- विभाजन गरेर लोड
- Set the buffer size and repeatedly call in a loop.
- स्मृति दक्षताको विचार
- Use dynamic memory allocation () to expand the buffer size as needed.
कोड उदाहरण:
char buffer[4096]; // ४KB को बफर
while ((bytesRead = read(fd, buffer, sizeof(buffer))) > 0) {
// डेटा प्रोसेस गर्ने
}
Q6. read
कार्यमा डेटा बीचमा किन टुट्छ?
उत्तर:
डेटा बीचमा टुट्ने कारणहरू तलका जस्ता हुन सक्छन्।
- आंशिक पठन
- It may happen that not all of the specified number of bytes are read at once. This behavior is commonly observed with sockets and pipes.
- सिग्नलको प्रभाव
- When a signal occurs, may be interrupted.
- गैर-रोक मोडको प्रभाव
- It occurs when the amount of available data is limited.
उपाय:
- It employs a design that repeats until the data is completely read.
- Properly handles signals and error codes.
FAQ सारांश
यी प्रश्न र उत्तरहरू मार्फत, read
कार्यसँग सम्बन्धित सामान्य समस्या र शंकाहरू समाधान भएको हुनुपर्छ। विशेष गरी, त्रुटि प्रक्रिया र विशेष प्रयोग स्थितिहरू (गैर-रोक मोड, EOF प्रक्रिया) को बारेमा पर्याप्त बुझाइ महत्वपूर्ण छ।
7. सारांश
यस लेखमा, C भाषा को read
कार्यको बारेमा, आधारभूत प्रयोगबाट लागू उदाहरणहरू, र ध्यान दिनुपर्ने बुँदा तथा FAQ सम्म विस्तृत रूपमा व्याख्या गरिएको छ। यस खण्डमा, सम्पूर्ण लेखको सामग्रीलाई पुनरावलोकन गरी, महत्वपूर्ण बुँदाहरूलाई व्यवस्थित गर्नेछौं।
read
कार्यको आधारभूत सारांश
- Overview:
read
- Syntax:
ssize_t read(int fd, void *buf, size_t count);
- Main Features:
- High flexibility, supporting a wide range of uses such as files, devices, and socket communication.
- It operates as a system call, and error handling and buffer management are required.
मुख्य प्रयोग उदाहरणहरू
- Reading from a file: We introduced a basic example of opening a file and reading its contents. We also explained how to read until EOF using loop processing.
- Data Acquisition from Standard Input: We presented a simple program that acquires user input using the function and outputs it.
- Receiving Data in Socket Communication: When receiving data between server/client, we have shown, with a specific example, the method of using .
उन्नत प्रयोग
- Asynchronous I/O:
fcntl
- Efficient Processing of Large Amounts of Data: To efficiently process large files or data, we explained the importance of setting an appropriate buffer size and considering memory management.
- Reading Binary Data: We explained how to safely read binary data using structures.
ध्यान दिनुपर्ने बुँदा र समस्या समाधान
- Buffer Overflow:
read
- Handling of EOF (End of File):
read
- Partial Reading: We explained how to handle cases where data can only be obtained partially in sockets or non-blocking mode.
FAQ द्वारा समाधान गरिएका मुख्य प्रश्नहरू
read
फंक्शन रfread
फंक्शनहरू बीचको भिन्नता
read
fread
- गैर‑ब्लकिङ मोड सेट गर्ने तरिका
fcntl
errno
- त्रुटि व्यवस्थापनको उत्कृष्ट अभ्यास
errno
यस लेखबाट सिकेका कुरा
read
फङ्सनको आधारभूत प्रयोग: फाइलहरू र इनपुट डिभाइसहरूबाट डेटा सुरक्षित रूपमा पढ्ने तरिका।- Practical Application Methods: Application examples in practical programming, such as asynchronous I/O and binary data processing.
- त्रुटि र समस्या समाधान: आंशिक पढाइ र EOF को सही प्रक्रियालाई बुझेर、मैले मजबुत कोड लेख्ने कौशल प्राप्त गरेको छु。
अर्को सिक्नुपर्ने कुरा
read
कार्य सिकेपछि अर्को चरणको रूपमा, तलका विषयहरूलाई गहिराइमा अध्ययन गर्दा बुझाइ अझ गहिरो हुनेछ।
write
फंक्शन: फाइल र उपकरणमा डेटा लेख्ने निम्न-स्तरको I/O फङ्शन।open
फङ्सन रclose
फंक्शन: फाइल सञ्चालनको आधारभूत संरचना सिक्नुहोस्।- Asynchronous Process: Learn more about event-driven programming and asynchronous I/O to create efficient programs.
अन्ततः
read
कार्य C भाषामा फाइल वा उपकरण संचालन गर्न आवश्यक उपकरण हो। यसको लचिलोपन र प्रदर्शनलाई अधिकतम बनाउन, सही प्रयोग र ध्यान दिनुपर्ने बुँदाहरूलाई बुझ्नु but महत्वपूर्ण छ। यो लेख but, शुरुआतीदेखि मध्यवर्ती प्रोग्रामरहरू सम्म, read
कार्यलाई पूर्ण रूपमा प्रयोग गर्न सहयोगी हुनेछ भन्ने आशा छ।