এক ফাইল হল ডিস্কে ফিজিক্যাল স্টোরেজ লোকেশন এবং ডিরেক্টরি হল ফাইলগুলো অর্গানাইজ করার জন্য একটি লজিক্যাল পাথ। একটি ফাইল একটি ডিরেক্টরির ভিতরে থাকে।
ফাইলে আমরা তিনটি অপারেশন পারফর্ম করতে পারি:
- ফাইল ওপেন করা।
- ফাইল প্রসেস করা (রিড, রাইট, মডিফাই)।
- ফাইল সেভ ও ক্লোজ করা।
অ্যালগরিদম
ফাইল থেকে একটি লাইন রিমুভ করার সি প্রোগ্রামের অ্যালগরিদম:
Step 1 − রানটাইমে ফাইল পাথ এবং রিমুভ করার লাইন নম্বর পড়ুন।
Step 2 − ফাইলটি রিড মোডে ওপেন করুন এবং সোর্স ফাইলে স্টোর করুন।
Step 3 − একটি টেম্পোরারি ফাইল রাইট মোডে তৈরি ও ওপেন করুন এবং এর রেফারেন্স টেম্পোরারি ফাইলে স্টোর করুন।
Step 4 − লাইন নম্বর ট্র্যাক করার জন্য count = 1 ইনিশিয়ালাইজ করুন।
Step 5 − সোর্স ফাইল থেকে একটি লাইন পড়ুন এবং বাফারে স্টোর করুন।
Step 6 − যদি করেন্ট লাইন রিমুভ করার লাইন নয় (line != count), তবে বাফার টেম্পোরারি ফাইলে লিখুন।
Step 7 − count++ করুন।
Step 8 − সোর্স ফাইল শেষ না হওয়া পর্যন্ত Step 5-7 রিপিট করুন।
Step 9 − দুটি ফাইল বন্ধ করুন (সোর্স এবং টেম্পোরারি)।
Step 10 − আসল সোর্স ফাইল ডিলিট করুন।
Step 11 − টেম্পোরারি ফাইলকে সোর্স ফাইল পাথ হিসেবে রিনেম করুন।
প্রোগ্রাম
ফাইল থেকে একটি লাইন রিমুভ করার সি প্রোগ্রাম:
#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