C++ 用C+;查找txt文件中最长的单词+;
我有一个.txt文件,如下所示:C++ 用C+;查找txt文件中最长的单词+;,c++,C++,我有一个.txt文件,如下所示: bear cat wolf mouse tiger elephant rat biggestword="o"; while (i=0;i<linesinfile;i++) { if (currentword.length>biggestword.length) { biggestword = currentword; } } 我需要编写一个简单的循环来读取我的txt文件并输出最长的单词。我该怎么办 biggest
bear
cat
wolf
mouse
tiger
elephant
rat
biggestword="o";
while (i=0;i<linesinfile;i++) {
if (currentword.length>biggestword.length) {
biggestword = currentword;
}
}
我需要编写一个简单的循环来读取我的txt文件并输出最长的单词。我该怎么办
biggestword="o";
while (i=0;i<linesinfile;i++) {
if (currentword.length>biggestword.length) {
biggestword = currentword;
}
}
编辑我遇到的问题是浏览文件中的每一行
biggestword="o";
while (i=0;i<linesinfile;i++) {
if (currentword.length>biggestword.length) {
biggestword = currentword;
}
}
biggestword=“o”;
while(i=0;ibiggestword.length){
最大单词=当前单词;
}
}
使用打开文件
biggestword="o";
while (i=0;i<linesinfile;i++) {
if (currentword.length>biggestword.length) {
biggestword = currentword;
}
}
std::ifstream file("filename.txt");
读每一行
biggestword="o";
while (i=0;i<linesinfile;i++) {
if (currentword.length>biggestword.length) {
biggestword = currentword;
}
}
while (std::getline(file, currentword)) { /* your code */ }
到目前为止你都尝试了什么?我写了伪代码,但我不知道如何实现我想要的。因此,这不是一个代码服务。试着自己解决它。在这里询问您的代码中定义的点。好吧,只需将每个单词读入一个单独的字符串,然后使用标准库获取每个字符串的长度。比较长度。通过使用
std::fstream
、std::string
和while
循环,可以轻松实现这一点