简体   繁体   中英

reading data from text files in c++ and changing the values

I am stuck at this normalization part. I am using a string and reading data one by one. I keep getting blank. The program compiles. Any hints to what to do next would be awesome. How would I complete the 5 steps below? Steps 3 and 4 work fine.

A program that reads a text file using character-by-character I/O and performs the following normalization tasks:

  1. Replaces all tab characters with 8 spaces
  2. Replaces all upper-case letters with lower-case letters
  3. All @ symbols will be replaced by the word "at".
  4. All = signs will be replaced by a series of 19 = signs.
  5. When you find an asterisk, you will print a series of asterisks. The character following the asterisk indicates the number of asterisks to print. Use the ASCII value of the character following. Number of asterisks is ASCII value minus 32 plus 1. The character following the asterisk is used only as a counter, not a data character.

.

#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";
    int count = 0;
    //Opening input and output file
    cout << "What file do you want to be normalized? \n";
    cin >> fname;
    cout << "\n";
    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 << "Your output file name is: " << ofname << "\n";
    //Normalization begins here
    char data;
    while (fin.get(data)) {
        if (data == "\t") { //***
            fout << "        ";
        }// else if (isupper(data)) { //***
           // fout << tolower(data);  //***

        else if (data == "@") {
            fout << "at";
        } else if (data == "=") {
            fout << "===================";
        } else if (data == "*") {
            fout << "some shit";
        }
}
    fin.close();
    fout.close();
    return 0;
}

[/code]

You were on the right track. Rather than a long winded explanation going line-by-line, I've included comments below. Your primary challenge is you were trying to read string data; where the intent of the problem seems to require char data; Also in reading character-by-character you need to include the stream modifier noskipws to insure you do not skip over whitespace characters. There are many, many ways to do this. This is just one example to compare against the approach you are taking:

#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;
}

Test Input:

$ 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).

Output:

$ 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM