การจัดการไฟล์ (File Handling) คือ การจัดเก็บข้อมูลในไฟล์โดยใช้โปรแกรม ในภาษา C โปรแกรมจะจัดเก็บผลลัพธ์และข้อมูลอื่นๆ ของโปรแกรมลงในไฟล์โดยใช้ file handling ใน C เรายังสามารถดึง/เรียกข้อมูลจากไฟล์มาใช้งานในโปรแกรมได้อีกด้วย
การดำเนินการที่คุณสามารถทำกับไฟล์ใน C มีดังนี้:
สร้างไฟล์ใหม่
เปิดไฟล์ที่มีอยู่แล้ว
อ่านข้อมูลจากไฟล์ที่มีอยู่
เขียนข้อมูลลงไฟล์
ย้ายข้อมูลไปตำแหน่งเฉพาะในไฟล์
ปิดไฟล์
สร้างหรือเปิดไฟล์ด้วย fopen()
ฟังก์ชัน fopen() ใช้สร้างไฟล์ใหม่หรือเปิดไฟล์ที่มีอยู่ใน C ฟังก์ชัน fopen ถูกนิยามในไฟล์ header stdio.h
ไวยากรณ์สำหรับการสร้างไฟล์ใหม่หรือเปิดไฟล์:
file = fopen("file_name", "mode")
นี่คือไวยากรณ์ทั่วไปสำหรับทั้งการเปิดและสร้างไฟล์ใน C
พารามิเตอร์
file_name - สตริงที่ระบุชื่อไฟล์ที่จะเปิดหรือสร้างโดยใช้เมธอด fopen
mode - สตริง (ปกติเป็นตัวอักษรตัวเดียว) ที่ระบุโหมดที่จะเปิดไฟล์ มีโหมดต่างๆ มากมายสำหรับเปิดไฟล์ใน C เราจะเรียนรู้ทุกโหมด בהמשך บทความนี้
เมื่อไหร่ไฟล์จะถูกสร้าง?
ฟังก์ชัน fopen จะสร้างไฟล์ใหม่เมื่อไม่พบไฟล์ชื่อที่ระบุในตำแหน่งที่ระบุ แต่ถ้าพบไฟล์ จะเปิดไฟล์ด้วยโหมดที่ระบุ
ตัวอย่าง: สมมติเราเปิดไฟล์ชื่อ hello.txt โดย fopen คำสั่งจะเป็น:
file = fopen("hello.txt", "w")
จะค้นหาไฟล์ hello.txt ในไดเรกทอรีปัจจุบัน ถ้ามีอยู่ จะเปิดไฟล์ มิฉะนั้นจะสร้างไฟล์ใหม่ชื่อ "hello.txt" และเปิดด้วยโหมดเขียน (ระบุด้วย "w")
ตอนนี้มาดูโหมดทั้งหมดที่ใช้ได้สำหรับอ่าน/เขียนไฟล์ใน C พร้อมตัวอย่างโค้ด
โหมด = "r"
เปิดเพื่ออ่านเท่านั้น โหมดนี้จะเปิดไฟล์เพื่ออ่านอย่างเดียว คือ เนื้อหาสามารถดูได้อย่างเดียว ไม่สามารถแก้ไขได้
โหมดนี้ ไม่สามารถสร้างไฟล์ใหม่ได้ และ fopen() จะคืนค่า NULL หากพยายามสร้างไฟล์ใหม่ด้วยโหมดนี้
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "r")) {
printf("File opened successfully in read mode");
} else
printf("The file is not present! cannot create a new file using r mode");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in read mode
เราสร้างไฟล์ hello.txt ไว้ในไดเรกทอรีปัจจุบัน แต่ถ้าพยายามเข้าถึงไฟล์อื่น จะได้ข้อความ "The file is not present! cannot create a new file using r mode"
โหมด = "rb"
เปิดเพื่ออ่านในโหมดไบนารี โหมดนี้จะเปิดไฟล์เพื่ออ่านในโหมดไบนารีอย่างเดียว
โหมดนี้ ไม่สามารถสร้างไฟล์ใหม่ได้ และ fopen() จะคืนค่า NULL หากพยายามสร้างไฟล์ใหม่ด้วยโหมดนี้
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("program.txt", "rb")) {
printf("File opened successfully in read mode");
} else
printf("The file is not present! cannot create a new file using rb mode");
fclose(file);
return 0;
}
ผลลัพธ์
The file is not present! cannot create a new file using rb mode
โหมด = "w"
เปิดเพื่อเขียนอย่างเดียว โหมดนี้จะเปิดไฟล์ถ้ามีในไดเรกทอรีปัจจุบันเพื่อเขียนอย่างเดียว คือ ไม่สามารถอ่านได้ ถ้าไม่มีไฟล์ โปรแกรมจะสร้างไฟล์ใหม่และเปิดเพื่อเขียน
ถ้าเปิดไฟล์ที่มีข้อความอยู่ เนื้อหาจะถูกเขียนทับ (overwrite)
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("helo.txt", "w")) {
printf("File opened successfully in write mode or a new file is created");
} else
printf("Error!");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in write mode or a new file is created
ที่นี่เราพยายามเปิดไฟล์ "helo.txt" ซึ่งไม่มีในไดเรกทอรี แต่ฟังก์ชันคืนค่าสำเร็จเพราะมันสร้างไฟล์ "helo.txt" ใหม่
โหมด = "wb"
เปิดเพื่อเขียนในโหมดไบนารี โหมดนี้จะเปิดไฟล์ถ้ามีในไดเรกทอรีปัจจุบันเพื่อเขียนในโหมดไบนารี คือ ไม่สามารถอ่านได้ ถ้าไม่มีไฟล์ โปรแกรมจะสร้างไฟล์ใหม่และเปิดเพื่อเขียนในโหมดไบนารี
ถ้าเปิดไฟล์ที่มีข้อความอยู่ เนื้อหาจะถูกเขียนทับ
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "wb")) {
printf("File opened successfully in write in binary mode or a new file is created");
} else
printf("Error!");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in write in binary mode or a new file is created
โหมด = "a"
เปิดเพื่อเพิ่ม (append) อย่างเดียว โหมดนี้จะเปิดไฟล์ถ้ามีในไดเรกทอรีปัจจุบันเพื่อเขียนอย่างเดียว คือ ไม่สามารถอ่านได้ ถ้าไม่มีไฟล์ โปรแกรมจะสร้างไฟล์ใหม่และเปิดเพื่อเขียน ถ้าเปิดไฟล์ที่มีข้อความอยู่ เนื้อหาจะ ไม่ถูกเขียนทับ แต่จะเพิ่มข้อความใหม่ต่อท้ายข้อความเดิม
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "a")) {
printf("File opened successfully in append mode or a new file is created");
} else
printf("Error!");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in append mode or a new file is created
โหมด = "ab"
เปิดเพื่อเพิ่มในโหมดไบนารี โหมดนี้จะเปิดไฟล์ถ้ามีในไดเรกทอรีปัจจุบันเพื่อเขียนในโหมดไบนารีอย่างเดียว ถ้าไม่มีไฟล์ โปรแกรมจะสร้างไฟล์ใหม่และเปิดเพื่อเขียนในโหมดไบนารี เนื้อหาเดิมจะไม่ถูกเขียนทับ จะเพิ่มต่อท้ายแทน
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "ab")) {
printf("File opened successfully in append in binary mode or a new file is created");
} else
printf("Error!");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in append in binary mode or a new file is created
โหมด = "r+"
เปิดเพื่ออ่านและเขียนทั้งคู่ โหมดนี้จะเปิดไฟล์เพื่ออ่านและเขียนได้ทั้งคู่ คือ สามารถทำทั้งการอ่านและเขียนกับไฟล์ได้
โหมดนี้ ไม่สามารถสร้างไฟล์ใหม่ได้ และ fopen() จะคืนค่า NULL หากพยายามสร้างไฟล์ใหม่ด้วยโหมดนี้
ถ้าเปิดไฟล์ที่มีข้อความอยู่และเขียนบางอย่าง เนื้อหาจะถูกเขียนทับ
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "r+")) {
printf("File opened successfully in read and write both");
} else
printf("The file is not present! cannot create a new file using r+ mode");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in read and write both
โหมด = "rb+"
เปิดเพื่ออ่านและเขียนในโหมดไบนารี โหมดนี้จะเปิดไฟล์เพื่ออ่านและเขียนในโหมดไบนารีได้ทั้งคู่
โหมดนี้ ไม่สามารถสร้างไฟล์ใหม่ได้ และ fopen() จะคืนค่า NULL หากพยายามสร้างไฟล์ใหม่ด้วยโหมดนี้
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("program.txt", "rb+")) {
printf("File opened successfully in read mode");
} else
printf("The file is not present! cannot create a new file using rb+ mode");
fclose(file);
return 0;
}
ผลลัพธ์
The file is not present! cannot create a new file using rb+ mode
โหมด = "w+"
เปิดเพื่อเขียนและอ่าน โหมดนี้จะเปิดไฟล์ถ้ามีในไดเรกทอรีปัจจุบันสำหรับเขียนและอ่านทั้งคู่ ถ้าไม่มีไฟล์ โปรแกรมจะสร้างไฟล์ใหม่และเปิดเพื่ออ่านและเขียน เนื้อหาเดิมจะถูกเขียนทับ
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("helo.txt", "w+")) {
printf("File opened successfully in read-write mode or a new file is created");
} else
printf("Error!");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in read-write mode or a new file is created
โหมด = "wb+"
เปิดเพื่อเขียนและอ่านในโหมดไบนารี โหมดนี้จะเปิดไฟล์ถ้ามีในไดเรกทอรีปัจจุบันสำหรับเขียนและอ่านในโหมดไบนารี ถ้าไม่มีไฟล์ โปรแกรมจะสร้างไฟล์ใหม่และเปิดเพื่ออ่านและเขียนในโหมดไบนารี เนื้อหาเดิมจะถูกเขียนทับ
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "wb+")) {
printf("File opened successfully in read-write in binary mode or a new file is created");
} else
printf("Error!");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in read-write in binary mode or a new file is created
โหมด = "a+"
เปิดเพื่ออ่านและเพิ่ม (append) โหมดนี้จะเปิดไฟล์ถ้ามีในไดเรกทอรีปัจจุบันสำหรับอ่านและเขียน ถ้าไม่มีไฟล์ โปรแกรมจะสร้างไฟล์ใหม่และเปิดเพื่ออ่านและเขียน เนื้อหาเดิมจะไม่ถูกเขียนทับ จะเพิ่มข้อความใหม่ต่อท้าย
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "a+")) {
printf("File opened successfully in read-append mode or a new file is created");
} else
printf("Error!");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in read-append mode or a new file is created
โหมด = "ab+"
เปิดเพื่ออ่านและเพิ่มในโหมดไบนารี โหมดนี้จะเปิดไฟล์ถ้ามีในไดเรกทอรีปัจจุบันสำหรับอ่านและเขียนในโหมดไบนารี ถ้าไม่มีไฟล์ โปรแกรมจะสร้างไฟล์ใหม่และเปิดเพื่ออ่านและเขียนในโหมดไบนารี เนื้อหาเดิมจะไม่ถูกเขียนทับ
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "ab+")) {
printf("File opened successfully in read-append in binary mode or a new file is created");
} else
printf("Error!");
fclose(file);
return 0;
}
ผลลัพธ์
File opened successfully in read-append in binary mode or a new file is created
การอ่านข้อมูลจากไฟล์ที่มีอยู่
เราสามารถอ่านเนื้อหาไฟล์ใน C ด้วยฟังก์ชัน fscanf(), fgets() และ fgetc() ทั้งหมดใช้เพื่ออ่านเนื้อหาไฟล์ มาดูการทำงานของแต่ละฟังก์ชัน
fscanf()
ฟังก์ชัน fscanf() ใช้เพื่ออ่านชุดตัวอักษร (strings) จากไฟล์ คืนค่า EOF เมื่ออ่านเนื้อหาไฟล์หมดแล้ว
Syntax
int fscanf(FILE *stream, const char *charPointer[])
Parameters
FILE *stream: พอยน์เตอร์ไปยังไฟล์ที่เปิดอยู่ const char *charPointer[]: สตริงของตัวอักษร
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
char str[500];
if (file = fopen("hello.txt", "r")) {
while (fscanf(file, "%s", str) != EOF) {
printf("%s", str);
}
} else
printf("Error!");
fclose(file);
return 0;
}
ผลลัพธ์
LearnprogrammingattutorialsPoint
fgets()
ฟังก์ชัน fgets() ใน C ใช้เพื่ออ่านสตริงจากสตรีม
Syntax
char* fgets(char *string, int length, FILE *stream)
Parameter
char *string: สตริงที่จะเก็บข้อมูลจากไฟล์ int length: จำนวนเต็มที่ให้ความยาวของสตริงที่จะพิจารณา FILE *stream: พอยน์เตอร์ไปยังไฟล์ที่เปิดอยู่
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
char str[500];
if (file = fopen("hello.txt", "r")) {
printf("%s", fgets(str, 50, file));
}
fclose(file);
return 0;
}
ผลลัพธ์
Learn programming at tutorials Point
fgetc()
ฟังก์ชัน fgetc() ใน C ใช้เพื่อคืนค่าตัวอักษรเดี่ยวจากไฟล์ รับตัวอักษรจากไฟล์และคืนค่า EOF ที่ท้ายไฟล์
Syntax
int fgetc(FILE *stream)
Parameter
FILE *stream: พอยน์เตอร์ไปยังไฟล์ที่เปิดอยู่
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
char ch;
if (file = fopen("hello.txt", "r")) {
while ((ch = fgetc(file)) != EOF)
printf("%c", ch);
}
fclose(file);
return 0;
}
ผลลัพธ์
Learn programming at tutorials Point
การเขียนข้อมูลลงไฟล์ใน C
เราสามารถเขียนข้อมูลลงไฟล์ใน C ด้วยฟังก์ชัน fprintf(), fputs(), fputc() ทั้งหมดใช้เพื่อเขียนเนื้อหาลงไฟล์
fprintf()
ฟังก์ชัน fprintf() ใช้เพื่อเขียนข้อมูลลงไฟล์ เขียนชุดตัวอักษรลงในไฟล์
Syntax
int fprintf(FILE *stream, char *string[])
Parameters
FILE *stream: พอยน์เตอร์ไปยังไฟล์ที่เปิดอยู่ char *string[]: อาร์เรย์ตัวอักษรที่เราต้องการเขียนในไฟล์
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "w")) {
if (fprintf(file, "tutorials Point") >= 0)
printf("Write operation successful");
}
fclose(file);
return 0;
}
ผลลัพธ์
Write operation successful
fputs()
ฟังก์ชัน fputs() ใน C ใช้เพื่อเขียนลงไฟล์ ใช้เขียนบรรทัด (character line) ลงในไฟล์
Syntax
int fputs(const char *string, FILE *stream)
Parameters
const char *string: อาร์เรย์ตัวอักษรที่เราต้องการเขียนในไฟล์ FILE *stream: พอยน์เตอร์ไปยังไฟล์ที่เปิดอยู่
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "w")) {
if (fputs("tutorials Point", file) >= 0)
printf("String written to the file successfully...");
}
fclose(file);
return 0;
}
ผลลัพธ์
String written to the file successfully...
fputc()
ฟังก์ชัน fputc() ใช้เพื่อเขียนตัวอักษรเดี่ยวลงในไฟล์
Syntax
int fputc(char character, FILE *stream)
Parameters
char character: ตัวอักษรที่เราต้องการเขียนในไฟล์ FILE *stream: พอยน์เตอร์ไปยังไฟล์ที่เปิดอยู่
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
if (file = fopen("hello.txt", "w")) {
fputc('T', file);
}
fclose(file);
return 0;
}
ผลลัพธ์
'T' is written to the file.
fclose()
ฟังก์ชัน fclose() ใช้เพื่อปิดไฟล์ที่เปิดอยู่ เราควรปิดไฟล์หลังทำการดำเนินการเพื่อบันทึกการเปลี่ยนแปลงที่เราทำไปกับมัน
Syntax
fclose(FILE *stream)
Parameters
FILE *stream: พอยน์เตอร์ไปยังไฟล์ที่เปิดอยู่
ตัวอย่าง
#include <stdio.h>
int main() {
FILE *file;
char string[300];
if (file = fopen("hello.txt", "a+")) {
while (fscanf(file, "%s", string) != EOF) {
printf("%s", string);
}
fputs("Hello", file);
}
fclose(file);
return 0;
}
ผลลัพธ์
LearnprogrammingatTutorialsPoint
เนื้อหาในไฟล์
Learn programming at Tutorials PointHello