রিডিং মোডে একটি ফাইল খুলুন। যদি ফাইলটি বিদ্যমান থাকে, তাহলে একটি ফাইলের লাইনের সংখ্যা গণনা করার জন্য একটি কোড লিখুন। যদি ফাইলটি বিদ্যমান না থাকে তবে এটি একটি ত্রুটি প্রদর্শন করে যে ফাইলটি সেখানে নেই৷
ফাইলটি রেকর্ডের একটি সংগ্রহ (বা) এটি হার্ড ডিস্কের একটি জায়গা যেখানে ডেটা স্থায়ীভাবে সংরক্ষণ করা হয়।
ফাইলগুলিতে সঞ্চালিত ক্রিয়াকলাপগুলি নিম্নরূপ -
-
ফাইলের নামকরণ
-
ফাইল খোলা হচ্ছে
-
ফাইল থেকে পড়া
-
ফাইলে লেখা
-
ফাইল বন্ধ করা হচ্ছে
সিনট্যাক্স
নিচে একটি ফাইল খোলার এবং নামকরণের জন্য সিনট্যাক্স রয়েছে -
1) FILE *File pointer;
Eg : FILE * fptr;
2) File pointer = fopen ("File name", "mode");
Eg : fptr = fopen ("sample.txt", "r");
FILE *fp;
fp = fopen ("sample.txt", "w"); প্রোগ্রাম 1
#include <stdio.h>
#define FILENAME "Employee Details.txt"
int main(){
FILE *fp;
char ch;
int linesCount=0;
//open file in read more
fp=fopen(FILENAME,"r");
if(fp==NULL){
printf("File \"%s\" does not exist!!!\n",FILENAME);
return -1;
}
//read character by character and check for new line
while((ch=getc(fp))!=EOF){
if(ch=='\n')
linesCount++;
}
//close the file
fclose(fp);
//print number of lines
printf("Total number of lines are: %d\n",linesCount);
return 0;
} আউটপুট
Total number of lines are: 3 Note: employee details.txt file consist of Pinky 20 5000.000000 Here total number of line are 3
প্রোগ্রাম 2
এই প্রোগ্রামে, আমরা দেখব কিভাবে একটি ফাইলে মোট লাইনের সংখ্যা খুঁজে বের করা যায় যেটি ফোল্ডারে নেই।
#include <stdio.h>
#define FILENAME "sample.txt"
int main(){
FILE *fp;
char ch;
int linesCount=0;
//open file in write mode
fp=fopen(FILENAME,"w");
printf ("enter text press ctrl+z of the end");
while ((ch = getchar( ))!=EOF){
fputc(ch, fp);
}
fclose(fp);
//open file in read more
fp=fopen(FILENAME,"r");
if(fp==NULL){
printf("File \"%s\" does not exist!!!\n",FILENAME);
return -1;
}
//read character by character and check for new line
while((ch=getc(fp))!=EOF){
if(ch=='\n')
linesCount++;
}
//close the file
fclose(fp);
//print number of lines
printf("Total number of lines are: %d\n",linesCount);
return 0;
} আউটপুট
enter text press ctrl+z of the end Hi welcome to Tutorials Point C Pogramming Question & answers ^Z Total number of lines are: 2