2012-03-14 10 views
0

私は、引数ベクトルを使用して2つのテキストファイルを取り込み、最初のものを入力、2番目のものを出力/空白ファイル)に変更し、の単語(単語のみ - 文字のみを含むとみなされる)または単語の文字を同じ文字数のの数字で変更して入力ファイルをデコードし、各数字を " 0 "、その他のもの(文字、数字、句読点)を" x "に置き換えます。デコードされたファイルを出力ファイルにコピーし、それぞれの文字列(単語、数値、混合)の間に同じ数のスペースと行を維持します。ファイル内の文字列の文字を別の特定の文字に変更することによってテキストファイルを別のものに復号する

注:入力の行は限定しないでください!

問題:それは各行 を終えた後の行を入力していない

は率直に言って、私は何かを書き、それが部分的に動作し、各文字列を読んだ後と復号化を行うと、出力ファイルにデコードされた複写そのため、同じ 行にあるように、連続してデコードされたすべての文字列が出力されます。ここで

はコードです:例えば

void read_line(ifstream &inp_f,ofstream &out_f) 
{ 
    char c, 
     str[MAX_STR_LEN],// holds the string that working on 
     prev_str[MAX_STR_LEN]; // holds the previous string 
    int count_spaces=-1, 
     count_entered=0, // how many chars are in the array including '\0' 
     counter[CNTR_LEN];// holds the number of each type of string in the line 
    bool read=false,copy=false; 

    while(!inp_f.eof()) 
    { 
     c=inp_f.get(); 

     if(!isspace(c) && c!='\n') 
     { 
      str[count_entered]=c; 
      count_entered++; 
      read=true;// only if something has been read except spaces in the line 
      copy=true;// used to give the command to copy the char to the string array 
     } 
     else if(isspace(c) || c=='\n')// when reading a space or one line 
     { 
      count_spaces++; 

      str[count_entered]='\0'; 

      if(copy && strcmp(prev_str,str)!=0) 
      { 
       strcpy(prev_str,str); 
       decode(counter,str);// Decoding the string considering it's type 
       copy_str(count_spaces,str,counter,out_f); // when all the string has been read,copy it to the output file 
       count_entered=0; 
       count_spaces=0; 
       copy=false; 
      } 
      ///////////////// can't take it to enter here ///////////////////////// 
      else if((!copy || !read) && c=='\n') // when finishing one line 
      { 
       out_f << endl; 
       // print how many strings there of each type in the current line 
       cout << counter[0] << " " << counter[1] << " " 
        << counter[2] << endl; 

       // re-initial the counters for the next line! 
       for(int i=0;i<CNTR_LEN;i++) 
       { 
        counter[i]=0; 
       } 
      } 
     } 
    } 
} 

入力:ちょうど11500年前に住んで

未知の人類は15で発見された中国南部、 で識別されています/ 03/12または先月3日

出力:

AAAAAAA AAAAA AAAAAAA AAAAAA AAAA 00000 AAAAAのaaa AAAのAAAAAAAAAA AA aaaaaaaaをXXXXXX AAAAAのAAAAAAAAAA AAのXXXXXXXXのAA、AAA xxxの単三AAAAのAAAAA

+0

のようになりますあなたはそれをファイルに書き込んで改行を得るか、書かれた文字列の後に '\ n'を書いていますか? – twain249

+0

私は分割された行にすべての文字列(単語や数字など)を必要としません!私は段落のように行にそれらをしたい! –

+0

それでは、何とか線のサイズを制限する必要があります。それぞれの行の長さを数え、ある点に達したときに '\ n'文字を追加することができます。 – twain249

答えて

0

2つのこと。

  1. あなたは誤ってテストしています。最初に読んでからeofをテストしなければなりません。最初にテストしてから、読んでいます。

  2. なぜ入力文字列と出力文字列を格納しているのかわかりません。あなたが必要とするのは、トークンにいくつの文字が含まれているかのカウントと、白い種類のトークンを追跡するためのブール値の数だけです。

は、これは宿題のように思えるので、私は実用的なソリューションを生産するつもりはないが、それはあなたが `\のN 'の前に文字列を追加しようとしたことがあり

char ch; 
int ch_cnt = 0; 
while (inp_f.get(ch)) { 
    ++ch_cnt; 
    if (isspace(ch)) { 
    // update the count for the right type of token 
    // output the symbols to replace the token 
    // clear all the type flags 
    // then output ch (to get a space) and set ch_cnt back to 0 
    } else if (isdigit(ch)) { 
    // it's either a number or mixed 
    } else if (isalpha(ch)) { 
    // it's either a word or mixed 
    } else { 
    // it's mixed 
    } 

    if ('\n' == ch) { 
    // output the counts of the token types 
    // set the counts back to 0 
    } 
} 
+0

まず最初に、それは返事とアイデアのための宿題と感謝です、今は2番目のことについて "なぜ私は文字列を保存しているのですか?" (2つ以上の単語がある場合は互いに正確にデコードしてコピーするのではなく、1つだけをコピーしてください!) –

関連する問題