Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading and writing files in C
#1
C provides functions for reading from and writing to files.

// Writing to a file
FILE *file = fopen("data.txt", "w");
if (file != NULL) {
    fprintf(file, "Hello, World!");
    fclose(file);
}

// Reading from a file
char buffer[100];
file = fopen("data.txt", "r");
if (file != NULL) {
    fgets(buffer, 100, file);
    printf("%s\n", buffer); // Output: Hello, World!
    fclose(file);
}
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing clean and maintainable code in C Qomplainerz 0 232 07-25-2023, 01:31 PM
Last Post: Qomplainerz
  Creating and using header files in C Qomplainerz 0 265 07-25-2023, 01:29 PM
Last Post: Qomplainerz
  Text and binary files in C Qomplainerz 0 243 07-25-2023, 01:24 PM
Last Post: Qomplainerz
  Writing your first "Hello, World!" program Qomplainerz 0 342 07-25-2023, 10:49 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 3 Guest(s)