Stuck at this error. Reading data from files

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#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 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
    string data;
    fin >> data;
    while (!fin.eof()) {
        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 >> data;
}
    fin.close();
    fout.close();
    return 0;
}
Last edited on
Whats with the duplicates around line 30?
You are reading in a string at a time. You really want to read in a character at a time. If the input is
myName@myURL
, the output should be
myNameatmyURL
, but if you read in a string at a time (lines 39 - 40), you will never find the '@' in the middle of the word. And since you don't want to ignore white space, don't use ">>", but use get() instead. (http://www.cplusplus.com/reference/istream/istream/get/)

After you read in characters rather than strings, your calls to isupper and tolower should work. Note that tolower will return its argument unchanged if it is not an uppercase letter, so it is not expressly necessary to call isupper if you format arrange your if / else if / else statements correctly.

After you read a '*' you need to read another character. The character can be used as an integer value, so use that value to print out a series of '*' So, if you read in
*!
, you should print out
**
because the ascii value of '!' is 33 (33 - 32 + 1 = 2).
Topic archived. No new replies allowed.