C++ 从c++;以及更改值

C++ 从c++;以及更改值,c++,C++,我被困在这一部分。我正在使用一个字符串并逐个读取数据。我一片空白。程序编译。任何关于下一步该做什么的提示都会非常棒。我如何完成下面的5个步骤?第3步和第4步很好 一种使用逐字符I/O读取文本文件并执行以下规范化任务的程序: 将所有制表符替换为8个空格 用小写字母替换所有大写字母 所有@符号将替换为“at” 所有=符号将被一系列19=符号替换 当您找到星号时,您将打印一系列星号。星号后面的字符表示要打印的星号数。使用以下字符的ASCII值。星号的数量是ASCII值减32加1。星号后面的字符仅用作计

我被困在这一部分。我正在使用一个字符串并逐个读取数据。我一片空白。程序编译。任何关于下一步该做什么的提示都会非常棒。我如何完成下面的5个步骤?第3步和第4步很好

一种使用逐字符I/O读取文本文件并执行以下规范化任务的程序:

  • 将所有制表符替换为8个空格
  • 用小写字母替换所有大写字母
  • 所有@符号将替换为“at”
  • 所有=符号将被一系列19=符号替换
  • 当您找到星号时,您将打印一系列星号。星号后面的字符表示要打印的星号数。使用以下字符的ASCII值。星号的数量是ASCII值减32加1。星号后面的字符仅用作计数器,而不是数据字符
  • #包括
    #包括
    #包括
    #包括
    使用名称空间std;
    int main(){
    流鳍;
    流式流量计;
    字符fname[256];
    名称为[256]的字符;
    字符规范[256]=“.normal”;
    整数计数=0;
    //打开输入和输出文件
    cout>fname;
    
    cout你的思路是正确的。我没有逐行冗长的解释,而是在下面添加了注释。你的主要挑战是你试图读取
    字符串数据;
    而问题的意图似乎需要
    字符数据;
    同样在逐字读取
    字符时,你需要包含流修饰符
    noskipws
    ,以确保您不会跳过空白字符。有很多很多方法可以做到这一点。这只是与您正在采取的方法进行比较的一个示例:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    using namespace std;
    
    int main () {
        ifstream fin;
        ofstream fout;
        char fname[256];
        char ofname[256];
        char norm[256] = ".normal";
        char eq[] = "===================";          // set a convenient 19 char sting of '='
    
    
        //Opening input and output file
        cout << endl << " Enter name of file to be normalized: ";
        cin >> fname;
        cout << endl;
        fin.open (fname);
        if (fin.fail ()) {
            cout << "Error opening input file! \n";
            return 0;
        }
        strcpy (ofname, fname);
        strcat (ofname, norm);
        fout.open (ofname);
        if (fout.fail ()) {
            cout << "Error opening output file! \n";
            return 0;
        }
        cout << endl << " Your output file name is: " << ofname << endl << endl;
    
        //Normalization begins here
        char data;                                  // declare data as 'char' not 'string'
        fin >> noskipws >> data;                    // read each char (including whitespace)
        while (!fin.eof ()) {
            switch (data)
            {
                case '\t' :                         // replace 'tab' by '8 chars'
                    fout << "        ";
                    break;
                case '@'  :                         // replace '@' by 'at'
                    fout << "at";
                    break;
                case '='  :                         // replace '=' by series of 19 '='
                    fout << eq;
                    break;
                case '*'  :                         // replace '*n' by series of (ascii n - 31) '*'
                    // fin >> count;
                    fin >> data;                    // read next value
                    if (fin.eof ())                 // test if eof set
                        break;
                        for (int it=0; it < data - 31; it++)    // output calculate number of asterisks
                            fout << '*';
                    break;
                default:                            // use default case to proccess all data and
                    if (isupper (data)) {           // test upper/lower-case.
                        char lc = tolower (data);
                    fout << lc;
                    } else {
                        fout << data;
                    }
            }
            fin >> noskipws >> data;               // read the next character
        }
        fin.close ();                              // close files & return
        fout.close ();
        return 0;
    }
    
    输出:

    $ cat dat/test.dat
    A program that reads a text:
        Tab '       ' ->  8 spaces.
        U-C letters -> l-c letters.
        All @ -> "at".
        All = signs -> a series of 19 = signs.
        All 'asterisks''n' like '*6'. -> n series of asterisks
        (Where Number of Asterisks is ASCII value minus 32 plus 1).
    
    $ cat dat/test.dat.normal
    a program that reads a text:
        tab '        ' ->  8 spaces.
        u-c letters -> l-c letters.
        all at -> "at".
        all =================== signs -> a series of 19 =================== signs.
        all 'asterisks''n' like '***********************'. -> n series of asterisks
        (where number of asterisks is ascii value minus 32 plus 1).
    

    您没有使用“逐字符I/O”。为此,您应该将
    data
    a
    char
    。如果我这样做,则输出文件中不会打印任何内容。请提供一个我可以使用的示例。@SimranCheema
    strcpy(of name,fname);strcat(of name,norm)你已经声明了<代码> >名称>代码> 256个字符,而<代码>规范>代码256个字符,可能产生512个字符。为什么要这样做?你使用C++,所以使用<代码> STD::字符串< /代码>。尝试将数据设为字符,不要使用<代码> > />代码,并使你的while循环如下:< CODe> 虽然(fin.get(data))
    @PaulMcKenzie当我尝试使用字符串时,我得到了大量错误。没有匹配的函数用于调用“std::basic\u ifstream::get(std::string&)”
    $ cat dat/test.dat.normal
    a program that reads a text:
        tab '        ' ->  8 spaces.
        u-c letters -> l-c letters.
        all at -> "at".
        all =================== signs -> a series of 19 =================== signs.
        all 'asterisks''n' like '***********************'. -> n series of asterisks
        (where number of asterisks is ascii value minus 32 plus 1).