【问题标题】:C++ read words with 5 & 10 letters save them in new fileC ++读取具有5和10个字母的单词将它们保存在新文件中
【发布时间】:2017-07-11 06:21:40
【问题描述】:

我试图构建一个读取现有文本文件的程序。长度为 5 和 10 个字符的单词会保存在一个新文件中。我构建了这个程序,它使用 if 语句运行良好,但我应该使用函数void FindTenAndFiveLetterWords(string word)。我对函数还不满意,所以我非常感谢你对如何使用函数来构建这个程序的帮助。

这是我没有函数的代码。

include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
string word;
ifstream openFile;
openFile.open ("WordList.txt");
ofstream newFile ("List.txt");
if (!openFile.is_open())
{
    cout <<"Couldn't open the file" <<endl;
    return 0;

}

while (openFile >> word)
{
    int wordLength10 = 10;
    int wordLength5 = 5;
    if (word.length() == wordLength10)
    {
        newFile << word << ' ';
    }
    else if (word.length() == wordLength5)
    {
        newFile << word << ' ';

    }


}
openFile.close();
newFile.close();
return 0;

}

【问题讨论】:

  • 您需要学习如何编写函数。就是这样。
  • 如何通过函数签名传递文件名

标签: c++ function file input output


【解决方案1】:
include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main ()
{
string word;
ifstream openFile;
openFile.open ("WordList.txt");
ofstream newFile ("List.txt");
if (!openFile.is_open())
{
    cout <<"Couldn't open the file" <<endl;
    return 0;

}

FindTenAndFiveLetterWords(openFile, newFile)

openFile.close();
newFile.close();
return 0;
}

void FindTenAndFiveLetterWords(ifstream openFile, ofstream newFile)
{
string word;
while (openFile >> word)
{
    int wordLength10 = 10;
    int wordLength5 = 5;
    if (word.length() == wordLength10)
    {
        newFile << word << ' ';
    }
    else if (word.length() == wordLength5)
    {
        newFile << word << ' ';

    }
}
}

【讨论】:

  • 你得到的错误是什么?是编译错误还是运行时错误?
  • 如果是编译错误,请尝试使用 FindTenAndFiveLetterWords(ifstream &openFile, ofstream &newFile) 更改 FindTenAndFiveLetterWords(ifstream openFile, ofstream newFile) 。注意变量名前使用的 &。
【解决方案2】:

你的函数应该是这样的:

void FindTenAndFiveLetterWords(ofstream &newFile, string word){
      if( (word.length() == 10) || (word.length() == 5) ){
           newFile << word << ' ';
      }
}

然后替换这段代码:

int wordLength10 = 10;
int wordLength5 = 5;
if (word.length() == wordLength10)
{
    newFile << word << ' ';
}
else if (word.length() == wordLength5)
{
    newFile << word << ' ';

}

使用函数调用:

FindTenAndFiveLetterWords(newFile, word);

【讨论】:

  • 我试过这个功能,但新文件中没有任何内容被复制,它是空的!
【解决方案3】:

试一试

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void FindTenAndFiveLetterWords(ifstream &openFile, ofstream &newFile)
{
     string word;
     while (openFile >> word)
     {        
          if (word.length() == 10 || word.length() == 5) {
                newFile << word << ' ';
          }
     }
}

int main ()
{

    ifstream openFile;
    openFile.open ("WordList.txt");
    ofstream newFile ("List.txt");
    if (!openFile.is_open())
    {
         cout <<"Couldn't open the file" <<endl;
         return -1;
    }
    FindTenAndFiveLetterWords(openFile, newFile);
    openFile.close();
    newFile.close();
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    • 2020-12-16
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多