Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> C++

सी ++ प्रोग्राम शब्द से फ़ाइल शब्द पढ़ने के लिए?

इस खंड में हम देखेंगे कि कैसे हम C++ का उपयोग करके फ़ाइल सामग्री को शब्द दर शब्द पढ़ सकते हैं। कार्य बहुत सरल है। फ़ाइल सामग्री को पढ़ने के लिए हमें फ़ाइल इनपुट स्ट्रीम का उपयोग करना होगा। फ़ाइल स्ट्रीम फ़ाइल नाम का उपयोग करके फ़ाइल खोलेगी, फिर फ़ाइलस्ट्रीम का उपयोग करके, प्रत्येक शब्द को लोड करें और इसे शब्द नामक एक चर में संग्रहीत करें। फिर प्रत्येक शब्द को एक-एक करके प्रिंट करें।

एल्गोरिदम

read_word_by_word(फ़ाइल नाम)

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) { //take word and print
      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

  1. सी ++ प्रोग्राम में बाइनरी सर्च?

    द्विआधारी खोज, जिसे अर्ध-अंतराल खोज, लॉगरिदमिक खोज या बाइनरी चॉप के रूप में भी जाना जाता है, एक खोज एल्गोरिथ्म है जो एक क्रमबद्ध सरणी के भीतर लक्ष्य मान की स्थिति का पता लगाता है। बाइनरी खोज लक्ष्य मान की तुलना सरणी के मध्य तत्व से करती है। यदि वे समान नहीं हैं, तो आधा जिसमें लक्ष्य झूठ नहीं बोल सकत

  1. C++ का उपयोग करके टेक्स्ट फ़ाइल से डेटा पढ़ें

    टेक्स्ट फ़ाइल से डेटा पढ़ने के लिए यह एक C++ प्रोग्राम है। इनपुट tpoint.txt is having initial content as “Tutorials point.” आउटपुट Tutorials point. एल्गोरिदम Begin    Create an object newfile against the class fstream.    Call open() method to open a file “tpoin

  1. सी ++ के साथ टेक्स्ट फ़ाइल कैसे पढ़ा जाए?

    टेक्स्ट फ़ाइल को पढ़ने के लिए यह एक C++ प्रोग्राम है। इनपुट tpoint.txt is having initial content as “Tutorials point.” आउटपुट Tutorials point. एल्गोरिदम Begin    Create an object newfile against the class fstream.    Call open() method to open a file “tpoint.txt