C++-এ ifstream ব্যবহার করে টেক্সট ফাইল থেকে ইন্টিজার পড়ার একটি উদাহরণ নিচে দেওয়া হলো।
উদাহরণ কোড
#include <fstream>
#include <iostream>
using namespace std;
int main() {
// অ্যারে আকার ইনিশিয়ালাইজ
int arr[30];
ifstream is("a.txt");
int cnt = 0;
int x;
// চেক করুন অ্যারে ইতিমধ্যে ফুল হয়নি এবং ফাইল থেকে ইন্টিজার পড়ুন
while (cnt < 30 && is >> x)
arr[cnt++] = x;
// অ্যারেতে সংরক্ষিত ইন্টিজারগুলো প্রিন্ট করুন
cout << "The integers are:" << "\n";
for (int i = 0; i < cnt; i++) {
cout << arr[i] << ' ';
}
// ফাইল বন্ধ করুন
is.close();
return 0;
}
আউটপুট
The integers are: 1 2 3 4 5 6 7