Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我不知道';我不明白如何使用toupper和isalpha与.txt文件字符串输入_C++ - Fatal编程技术网

C++ 我不知道';我不明白如何使用toupper和isalpha与.txt文件字符串输入

C++ 我不知道';我不明白如何使用toupper和isalpha与.txt文件字符串输入,c++,C++,我的程序在很大程度上是不完整的,但我认为使用toupper修改从文本文件输入的字符串可以将所述字符串的所有字符更改为大写。我读到了与int类型一起工作的代码,或者我假设在本例中是Ascii。但我真的不确定接下来该怎么办。从C++文件中转换和检查字符串的最有效方法是什么? 我在谷歌上搜索了这个问题,找到了各种各样的方法,比如使用std::transform,但我觉得我的导师只希望我们使用Tony Gaddis的《从C++开始》一书中教给我们的方法。不幸的是,在我的书中,我没有找到任何关于toupp

我的程序在很大程度上是不完整的,但我认为使用toupper修改从文本文件输入的字符串可以将所述字符串的所有字符更改为大写。我读到了与int类型一起工作的代码,或者我假设在本例中是Ascii。但我真的不确定接下来该怎么办。从C++文件中转换和检查字符串的最有效方法是什么? 我在谷歌上搜索了这个问题,找到了各种各样的方法,比如使用std::transform,但我觉得我的导师只希望我们使用Tony Gaddis的《从C++开始》一书中教给我们的方法。不幸的是,在我的书中,我没有找到任何关于toupper用于文本文件输入的参考资料,只是用于char值。为了教育的缘故,我希望你们知道我在这里应该用什么,为什么。我欢迎推荐阅读

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

int main()
{
    string text;
    ifstream textFile;
    int ch;
    textFile.open("letter_count.txt", ios::in);
    toupper(textFile.get());
    isalpha(textFile.get());

    if (textFile)
    {
        // Read an item from the file.
        getline(textFile, text);

        // While the last read operation
        // was successful, continue.
        while (textFile)
        {
            if (textFile)
            // Display the last item read.
            cout << text << endl;

            // Read the next item.
            getline(textFile, text);
        }

        // Close the file.
        textFile.close();
    }
    else
    {
        cout << "ERROR: Cannot open file. \n";
    }
    cout << "The most common letter is " " with " " occurances. \n";
    cout << "The least common letter is " " with " " occurances. \n";


    system("pause");
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串文本;
ifstream文本文件;
int-ch;
打开(“letter_count.txt”,ios::in);
toupper(textFile.get());
isalpha(textFile.get());
如果(文本文件)
{
//从文件中读取项目。
getline(textFile,text);
//而上次读取操作
//如果成功,请继续。
while(文本文件)
{
如果(文本文件)
//显示上次读取的项目。

cout这是因为toupper只接受一个int值。请注意:


您必须循环字符串中的每个字符才能转换它。

std::transform(std::begin(str),std::end(str),std::begin(str),::toupper)在C/C++中,这是一种习惯用法。如果你的导师希望有其他的东西,请提问。当然,它有助于理解什么是<代码> STD::Trime:它基本上只是在指定范围内运行一个循环。请看详细的描述。你查过如何使用<代码> ToupPeor()
在这样一本参考手册中?另外,您可以使用
if(ifstream textFile{“letter_count.txt”}{while(getline(textFile,text)){…用text做点什么…}
-无需
。打开
。关闭
(这在
ifstream
析构函数中自动完成),您不需要两个
getline
调用,也不需要一个
while(textFile)
后跟
if(textFile)
if
永远不会失败,因为
while
循环如果
textFile
不好就不会进入)谢谢,托尼,我的书中就用了这个例子。我想知道为什么他们这样做是因为效率低下?