如何使用函数定义打开、读取和关闭文件

布鲁克奥

我们被要求打开一个包含句子的文本文件,并查看文件中的所有字母和空格,并计算每个 ascii 字符的数量。

当我在主函数中拥有所有信息时,它工作正常。我只是不知道如何在主要内容中成功调用它们。输出应如下所示:

15 words  
6 a   
3 d  
6 e  
3 g  
3 h  
3 i  
15 l  
6 o  
6 r  
3 s  
3 t  
3 w  

当一切都在主函数中时,我实现了这个输出,所以现在只需要通过我的函数定义来获得它。

我的代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
    
//prototypes
//int openFile();
void readFile(ifstream &in);
void closeFile(ifstream &in);
    
//definitions
int openFile()
{
    ifstream in;
    in.open("word_data.txt");
}

void readFile(ifstream &in)
{
    //store the frequency of the letters
    int letters[128];
    
    //declare variables
    char let;
    int wordCount = 0;
    
    //for loop to initialize all counts to zero
    for (int i = 0; i < 128; i++)
    {
        letters[i] = 0;
    }

    //get letters until we reach end of file
    //whitespace = wordCount++;
    let = in.get();
    while (let != EOF)
    {
        if (let == ' ')
            wordCount++;
        
        //change to lowercase
        let = tolower(let);
        
        letters[let]++;
        let = in.get();
    }
    
    //output
    //num words
    cout << wordCount + 1 << " words" << endl;
        
    //count how many of each letter there are & print to screen in alphabetical order
    for (char let = 'a'; let <= 'z'; let++)
    {
        if (letters[let] != 0)
        {
            cout << letters[let] << " "<< let <<endl;
        }
    }
}

void closeFile(ifstream &in)
{
    in.close();
}
    
int main()
{
    openFile();
    readFile(in);
    closeFile(in);
    return 0;
}
雷米勒博

问题在于您的openFile()功能。它只创建一个本地 ifstream,它不打开ifstream其他功能可以访问的。

试试这个:

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
    
//prototypes
void openFile(ifstream &in);
void readFile(ifstream &in);
void closeFile(ifstream &in);
    
//definitions
void openFile(ifstream &in)
{
    in.open("word_data.txt");
}

void readFile(ifstream &in)
{
    //store the frequency of the letters
    int letters[128] = {};
    
    //declare variables
    char ch;
    int wordCount = 0;

    //get letters until we reach end of file
    while (in.get(ch))
    {
        if (ch == ' ')
            wordCount++;
        
        //change to lowercase
        ch = static_cast<char>(tolower(static_cast<unsigned char>(ch)));
        
        letters[ch]++;
    }
    
    //output
    //num words
    cout << wordCount + 1 << " words" << endl;
        
    //count how many of each letter there are & print to screen in alphabetical order
    for (ch = 'a'; ch <= 'z'; ch++)
    {
        if (letters[ch] != 0)
        {
            cout << letters[ch] << " " << ch <<endl;
        }
    }
}

void closeFile(ifstream &in)
{
    in.close();
}
    
int main()
{
    ifstream in;
    openFile(in);
    readFile(in);
    closeFile(in);
    return 0;
}

话虽如此,您可能会考虑使用 astd::map来跟踪您的频率,而不是使用int[]数组。并使用一次operator>>阅读整个单词:

#include <map>
...

void readFile(ifstream &in)
{
    //store the frequency of the letters
    map<char, int> letters;
    
    //declare variables
    string word;
    int wordCount = 0;

    //get letters until we reach end of file
    while (in >> word)
    {
        ++wordCount;
        
        //for(size_t idx = 0; idx < word.size(); ++idx)
        //{
        //    char ch = word[idx];
        for(char ch : word)
        {
            //change to lowercase
            ch = static_cast<char>(tolower(static_cast<unsigned char>(ch)));
        
            if (ch >= 'a' && ch <= 'z')
                letters[ch]++;
        }
    }

    //output
    //num words
    cout << wordCount << " words" << endl;
        
    //count how many of each letter there are & print to screen in alphabetical order
    //for (map<char, int>::iterator iter = letters.begin(); iter != letters.end(); ++iter)
    //{
    //    cout << iter->second << " " << iter->first << endl;
    //}
    for (auto &elem : letters)
    {
        cout << elem.second << " " << elem.first << endl;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何打开,读取和编辑使用在线Java编译器我的系统(.txt文件)的文件?

如何关闭单个Excel文件,而不是关闭当前使用Java打开的所有Excel文件

如何使用Zurb Foundation显示打开,打开,关闭,关闭回调函数?

grails应用程序中的“打开的文件太多”-如何正确关闭打开的文件和流?

如何从批处理文件打开和关闭Internet Explorer?

如何使用Swift关闭打开的文件?

如何打开和读取JSON文件?

如何使用Excel VBA打开文件并关闭更新链接

OleDbConnection:如何使用函数打开和关闭连接

如何使用Azure函数Blob触发器和C#从Blob存储读取json文件

如何在MacOS中使用bash打开和关闭应用

如何在不刷新PHP的情况下打开,读取,关闭,更新,重新打开和读取文件

如何使用lambda函数和boto3从s3存储桶读取csv文件?

以mips打开和读取文件

如何使用相同的热键打开和关闭模态

在C ++函数中,如何读取在main()中打开的输入文件?

Linux如何区分以读取模式打开文件和实际从文件读取数据之间的区别?

如何打开和关闭屏幕?

使用自定义TableModel和从文件中读取的项目列表时,如何更新jtable

PL / I-如何循环读取文件而不多次打开/关闭文件

如何使用 DropDown 加载和打开文件?

使用 Peewee ORM 从多个函数打开和关闭连接

如何打开和读取多个 .txt 文件

如何关闭使用 os.startfile()、Python 3.6 打开的文件

如何关闭使用 ast.literal_eval 打开它们的文件?

如何关闭在 API 调用中打开以读取的文件

如何在 Django 中打开和读取 FileField 类型的文件?

如何打开和关闭灯泡:但只使用 1 个按钮?

如何在函数中读取文件后关闭文件