यह मानते हुए कि file.txt में रैंडम फाइलों के नाम इस प्रकार हैं:
a.cpp
b.txt
c.java
d.cpp
...
विचार यह है कि मैं फ़ाइल एक्सटेंशन को फ़ाइल नाम से सबस्ट्रिंग के रूप में अलग करना चाहता हूं, और फिर डुप्लिकेट देखने के लिए एक्सटेंशन के बीच तुलना करना चाहता हूं।
यहाँ मेरा कोड है:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream infile;
infile.open("file.txt");
string str,sub;
int count,pos=0;
while(infile>>str)
{
pos=str.find(".");
sub=str.substr(pos+1);
if(sub==?)
// I stopped here
count++;
}
cout<<count;
return 0;
}
मैं सी ++ के लिए नया हूं इसलिए मुझे नहीं पता कि अगली पंक्ति पर जाने के लिए किस फ़ंक्शन का उपयोग करना है, मैंने इसे समझने के लिए बहुत कुछ खोजा, लेकिन कुछ भी नहीं।
1 उत्तर
आप इनपुट फ़ाइल में प्रत्येक एक्सटेंशन से संबंधित गणना मुद्रित करने के लिए निम्न प्रोग्राम का उपयोग कर सकते हैं। गिनती का ट्रैक रखने के लिए प्रोग्राम std::map
का उपयोग करता है।
#include <iostream>
#include <map>
#include <fstream>
int main()
{
std::ifstream inputFile("input.txt");
std::map<std::string, int> countExtOccurence; //this will count how many time each extension occurred
std::string name, extension;
if(inputFile)
{
while(std::getline(inputFile, name, '.')) //this will read upto a . occurrs
{
std::getline(inputFile, extension, '\n');
{
countExtOccurence[extension]++; //increase the count corresponding to a given extension
}
}
}
else
{
std::cout<<"input file cannot be opened"<<std::endl;
}
inputFile.close();
//lets print out how many times each extensino occurred in the file
for(const std::pair<std::string, int> &pairElem: countExtOccurence)
{
std::cout<<pairElem.first<<" occurred: "<<pairElem.second<<" time"<<std::endl;
}
return 0;
}
उपरोक्त प्रोग्राम का आउटपुट यहां देखा जा सकता है।
संबंधित सवाल
नए सवाल
c++
C ++ एक सामान्य-प्रयोजन प्रोग्रामिंग भाषा है। यह मूल रूप से C के विस्तार के रूप में डिज़ाइन किया गया था और इसमें एक समान सिंटैक्स है, लेकिन यह अब पूरी तरह से अलग भाषा है। C ++ कंपाइलर के साथ संकलित कोड के बारे में प्रश्नों के लिए इस टैग का उपयोग करें। विशिष्ट मानक संशोधन [C ++ 11], [C ++ 14], [C ++ 17], [C ++ 20] या [C ++ 23], आदि से संबंधित प्रश्नों के लिए संस्करण-विशिष्ट टैग का उपयोग करें। ।