এই সেকশনে আমরা দেখব কীভাবে আমরা C++ ব্যবহার করে ফাইল কন্টেন্ট ওয়ার্ড বাই ওয়ার্ড পড়তে পারি। টাস্কটি খুব সহজ। আমাদের ফাইল ইনপুট স্ট্রিম ব্যবহার করতে হবে ফাইল কন্টেন্ট পড়তে। ফাইল স্ট্রিম ফাইলনেম ব্যবহার করে ফাইলটি ওপেন করবে, তারপর FileStream ব্যবহার করে প্রতিটি ওয়ার্ড লোড করবে এবং একটি word ভেরিয়েবলে স্টোর করবে। তারপর প্রতিটি ওয়ার্ড একে একে প্রিন্ট করবে।
অ্যালগরিদম
read_word_by_word(filename)
begin
file = open file using filename
while file has new word, do
print the word into the console
done
end
ফাইল কন্টেন্ট (test_file.txt)
This is a test file. There are many words. The program will read this file word by word
উদাহরণ
#include<iostream>
#include<fstream>
using namespace std;
void read_word_by_word(string filename) {
fstream file;
string word;
file.open(filename.c_str());
while(file >> word) { // ওয়ার্ড নিন এবং প্রিন্ট করুন
cout << word << endl;
}
file.close();
}
main() {
string name;
cout << "Enter filename: ";
cin >> name;
read_word_by_word(name);
}
আউটপুট
Enter filename: test_file.txt This is a test file. There are many words. The program will read this file word by word