2014-10-26 44 views
0

我被困在這個標準化部分。我正在使用一個字符串並逐一讀取數據。我一直變得空白。該程序編譯。接下來要做什麼的任何提示都會很棒。我將如何完成下面的5個步驟?步驟3和4工作正常。從C++中的文本文件中讀取數據並更改值

讀取使用字符逐字符I/O的文本文件,並且執行下面的歸一化的任務的程序:

  1. 替換所有tab字符具有8位
  2. 替換與所有大寫字母小寫字母
  3. All @符號將被替換爲單詞「at」。
  4. 所有=標誌將被替換爲一系列19 =標誌。
  5. 當您找到星號時,您將打印一系列星號。星號後面的字符表示要打印的星號的數量。使用以下字符的ASCII值。星號的數量是ASCII值減去32加1.星號後面的字符僅用作計數器,而不是數據字符。

#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

您沒有使用「字符逐字符I/O「。要做到這一點,你應該把'data'變成'char'。 – ooga 2014-10-26 22:13:11

+0

如果我這樣做,那麼沒有什麼被打印到輸出文件。有一個我可以使用的例子。 – 2014-10-26 22:21:44

+1

@SimranCheema'strcpy(ofname,fname); strcat(ofname,norm);'可能溢出。您已將'ofname'聲明爲256個字符,將'norm'聲明爲256個字符,從而可以生成512個字符。爲什麼要這樣呢?你正在使用C++,所以使用'std :: string'。 – PaulMcKenzie 2014-10-26 22:36:02

回答

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