Introduction
File modes tell the computer how you want to open a file, like reading, writing, or adding new data.
FILE *fopen(const char *filename, const char *mode);
FILE *file = fopen("data.txt", "r");
FILE *file = fopen("output.txt", "w");
FILE *file = fopen("log.txt", "a");
FILE *file = fopen("data.bin", "rb");
#include <stdio.h> int main() { FILE *file = fopen("example.txt", "w"); if (file == NULL) { printf("Failed to open file.\n"); return 1; } fprintf(file, "Hello, file modes!\n"); fclose(file); file = fopen("example.txt", "r"); if (file == NULL) { printf("Failed to open file.\n"); return 1; } char buffer[50]; if (fgets(buffer, sizeof(buffer), file) != NULL) { printf("Read from file: %s", buffer); } fclose(file); return 0; }
data.txt for writing in C?fopen opens files; "w" mode opens for writing.open.FILE *fp = fopen("test.txt", "w");
fputs("Hello", fp);
fclose(fp);
fp = fopen("test.txt", "a");
fputs(" World", fp);
fclose(fp);
fp = fopen("test.txt", "r");
char buffer[20];
fgets(buffer, 20, fp);
printf("%s", buffer);
fclose(fp);FILE *fp;
fp = fopen("log.txt", "wa");
if (fp == NULL) {
printf("Failed to open file\n");
}Pass the test and this pattern moves unknown