我被困在这个标准化部分。我正在使用字符串并逐个读取数据。我一直空白。该程序编译。任何关于下一步该做什么的提示都会很棒。我如何完成以下5个步骤?第3步和第4步工作正常。
使用逐个字符的I / O读取文本文件并执行以下规范化任务的程序:
#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;
}
[/代码]
答案 0 :(得分:0)
你走在正确的轨道上。我没有逐行解读,而是在下面添加了评论。您面临的主要挑战是尝试阅读string data;
,问题的目的似乎需要char data;
同样在阅读character-by-character
时,您需要包含流修饰符 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).