ไฟล์คือตำแหน่งหน่วยเก็บข้อมูลจริงบนดิสก์ และไดเร็กทอรีเป็นพาธแบบลอจิคัลที่ใช้ในการจัดระเบียบไฟล์ มีไฟล์อยู่ภายในไดเร็กทอรี
การดำเนินการสามอย่างที่เราสามารถทำได้ในไฟล์มีดังนี้ -
- เปิดไฟล์
- ไฟล์ประมวลผล (อ่าน เขียน แก้ไข)
- บันทึกและปิดไฟล์
อัลกอริทึม
อัลกอริทึมได้รับด้านล่างเพื่ออธิบายโปรแกรม C เพื่อลบบรรทัดออกจากไฟล์
ขั้นตอนที่ 1 − อ่านเส้นทางของไฟล์และหมายเลขบรรทัดที่จะลบเมื่อรันไทม์
ขั้นตอนที่ 2 − เปิดไฟล์ในโหมดอ่านและเก็บไว้ในไฟล์ต้นฉบับ
ขั้นตอนที่ 3 − สร้างและเปิดไฟล์ชั่วคราวในโหมดเขียนและจัดเก็บข้อมูลอ้างอิงในไฟล์ชั่วคราว
ขั้นตอนที่ 4 − เริ่มต้นการนับ =1 เพื่อติดตามหมายเลขบรรทัด
ขั้นตอนที่ 5 − อ่านบรรทัดจากไฟล์ต้นฉบับและเก็บไว้ในบัฟเฟอร์
ขั้นตอนที่ 6 − หากบรรทัดปัจจุบันไม่เท่ากับบรรทัดที่ต้องการลบ เช่น if (line !=count) ให้เขียนบัฟเฟอร์ลงในไฟล์ชั่วคราว
ขั้นตอนที่ 7 − จำนวนที่เพิ่มขึ้น++.
ขั้นตอนที่ 8 − ทำซ้ำขั้นตอนที่ 5-7 จนจบซอร์สไฟล์
ขั้นตอนที่ 9 − ปิดทั้งสองไฟล์ เช่น ไฟล์ต้นฉบับและไฟล์ชั่วคราว
ขั้นตอนที่ 10 − ลบไฟล์ต้นฉบับของเรา
ขั้นตอนที่ 11 − เปลี่ยนชื่อไฟล์ชั่วคราวด้วยพาธของไฟล์ต้นทาง
โปรแกรม
ต่อไปนี้เป็นโปรแกรม C เพื่อ ลบบรรทัดออกจากไฟล์ −
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 1000
void deleteLine(FILE *src, FILE *temp, const int line);
void printFile(FILE *fptr);
int main(){
FILE *src;
FILE *temp;
char ch;
char path[100];
int line;
src=fopen("cprogramming.txt","w");
printf("enter the text.press cntrl Z:\n");
while((ch = getchar())!=EOF){
putc(ch,src);
}
fclose(src);
printf("Enter file path: ");
scanf("%s", path);
printf("Enter line number to remove: ");
scanf("%d", &line);
src = fopen(path, "r");
temp = fopen("delete.tmp", "w");
if (src == NULL || temp == NULL){
printf("Unable to open file.\n");
exit(EXIT_FAILURE);
}
printf("\nFile contents before removing line.\n\n");
printFile(src);
// Move src file pointer to beginning
rewind(src);
// Delete given line from file.
deleteLine(src, temp, line);
/* Close all open files */
fclose(src);
fclose(temp);
/* Delete src file and rename temp file as src */
remove(path);
rename("delete.tmp", path);
printf("\n\n\nFile contents after removing %d line.\n\n", line);
// Open source file and print its contents
src = fopen(path, "r");
printFile(src);
fclose(src);
return 0;
}
void printFile(FILE *fptr){
char ch;
while((ch = fgetc(fptr)) != EOF)
putchar(ch);
}
void deleteLine(FILE *src, FILE *temp, const int line){
char buffer[BUFFER_SIZE];
int count = 1;
while ((fgets(buffer, BUFFER_SIZE, src)) != NULL){
if (line != count)
fputs(buffer, temp);
count++;
}
} ผลลัพธ์
เมื่อโปรแกรมข้างต้นทำงาน มันจะให้ผลลัพธ์ดังต่อไปนี้ −
enter the text.press cntrl Z: Hi welcome to my world This is C programming tutorial You want to learn C programming Subscribe the course in TutorialsPoint ^Z Enter file path: cprogramming.txt Enter line number to remove: 2 File contents before removing line. Hi welcome to my world This is C programming tutorial You want to learn C programming Subscribe the course in TutorialsPoint File contents after removing 2 line. Hi welcome to my world You want to learn C programming Subscribe the course in TutorialsPoint