C के fread() और fwrite()
In this page:
fwrite(&data, sizeof(data_type), count, file_pointer);
fread(&data, sizeof(data_type), count, file_pointer);
fwrite() से Binary Blocks लिखना
क्योंकि यह formatted text की बजाय raw bytes लिखता है, fwrite() floats या structs जैसे data का exact binary representation preserve करता है, fprintf() के उलट जो पहले सब कुछ human-readable text में convert करता है।
उदाहरण: Writing binary blocks with fwrite()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int numbers[3] = {1, 2, 3};
FILE *fp = fopen("data.bin", "wb");
fwrite(numbers, sizeof(int), 3, fp);
fclose(fp);
// Print formatted text to the screen
printf("Wrote raw binary data");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
fread() से Binary Blocks पढ़ना
अगर fread() आपके मांगे से कम elements return करे, इसका आमतौर पर मतलब है आप बीच में file के अंत तक पहुँच गए -- हमेशा इस return value को check करें, यह मान लेने की बजाय कि पूरी मांगी गई मात्रा successfully पढ़ी गई।
उदाहरण: Reading binary blocks with fread()
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int numbers[3] = {1, 2, 3};
FILE *fp = fopen("data.bin", "wb");
fwrite(numbers, sizeof(int), 3, fp);
fclose(fp);
int result[3];
fp = fopen("data.bin", "rb");
fread(result, sizeof(int), 3, fp);
fclose(fp);
// Print formatted text to the screen
printf("%d", result[1]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Files में Structures लिखना
यह separate fprintf() calls से individually एक structure का हर member लिखने से कहीं ज़्यादा efficient है, क्योंकि fwrite() structure के raw memory layout को एक single operation में disk पर copy करता है।
उदाहरण: Writing structures to files
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x, y; };
// Program execution starts in main()
int main() {
struct Point p = {3, 4};
FILE *fp = fopen("point.bin", "wb");
fwrite(&p, sizeof(struct Point), 1, fp);
fclose(fp);
// Print formatted text to the screen
printf("Struct written in one operation");
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Files से Structures पढ़ना
यह reliably सिर्फ तब काम करता है जब reading program वही exact structure definition (compiler, platform, और padding/alignment समेत) इस्तेमाल करे जो file लिखने वाले program ने किया -- binary file formats बिना extra care के अलग compilers या architectures में portable नहीं हैं।
उदाहरण: Reading structures from files
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x, y; };
// Program execution starts in main()
int main() {
struct Point p = {3, 4};
FILE *fp = fopen("point.bin", "wb");
fwrite(&p, sizeof(struct Point), 1, fp);
fclose(fp);
struct Point result;
fp = fopen("point.bin", "rb");
fread(&result, sizeof(struct Point), 1, fp);
fclose(fp);
// Print formatted text to the screen
printf("%d %d", result.x, result.y);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Read/Write Count Verify करना
returned count को अपनी expected count से compare करना एक partial या failed read/write detect करने का इकलौता reliable तरीका है, क्योंकि ये functions अन्यथा उस तरह error raise नहीं करते जैसे कुछ दूसरे I/O functions करते हैं।
उदाहरण: Verifying read/write count
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int numbers[3] = {1, 2, 3};
FILE *fp = fopen("data.bin", "wb");
size_t written = fwrite(numbers, sizeof(int), 3, fp);
fclose(fp);
// Print formatted text to the screen
printf("%zu", written);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- mode में
bके बिना एक binary file खोलना, ताकि text-mode translation कुछ systems पर data corrupt कर सके। - pointers रखने वाले structs लिखना, जो actual data की बजाय addresses store करता है।
- लिखे गए bytes या items से अलग संख्या पढ़ना।
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: