2017-04-16 5 views
0

ファイルから文字を読み込んで文字配列に格納し、新しいファイルですが、すべての文字の生の数字が1つ増えています(ほとんどの経験豊富なプログラマーが私の話を知っています)。だから私は基本的に私自身の暗号化アルゴリズムを作ろうとしている。しかし、私は非常に奇妙なエラーが発生します: <various paths here>\main.cpp|27|error: no match for 'operator<<' (operand types are 'std::ifstream {aka std::basic_ifstream<char>}' and 'char')| 私はこのエラーについて多くのことを聞いたことがありますが、私が見ているのは、人々がクラス定義の関数を使用したときに起こることです。ソースコードは以下の通りです <various paths here>\main.cpp|27|note: no known conversion for argument 1 from 'std::ifstream {aka std::basic_ifstream<char>}' to 'int'|fstreamを使用してファイルに文字を書き込もうとしました: 'operator <<'と一致しません

#include <iostream> 
#include <fstream> 
using namespace std; 

int main() { 
    int array_size = 1024000; 
    char Str[array_size]; 
    int position = 0; 
    long double fsize = position; 

    ifstream finput("in.txt"); 
    ifstream foutput("out.txt"); 
    if(finput.is_open()) { 
     cout << "File Opened successfully. Storing file data into array." << endl; 
     while(!finput.eof() && position < array_size) { 
      finput.get(Str[position]); 
      position++; 
      fsize = position; 
      cout << "Processed data: " << fsize/1000 << "KB" << endl; 
     } 
     Str[position-1] = '\0'; 

     cout << "Storing done, encoding..." << endl << endl; 
     for(int i = 0; Str[i] != '\0'; i++) { 
      cout << Str[i] << "changed to " << char(Str[i] + 1) << endl; 
      foutput << Str[i] += 1; 
     } 

    } else { 
     cout << "File could not be opened. File must be named in.txt and must not be in use by another program." << endl; 
    } 
    return 0; 
} 

注:私は(出力文字列にfstreamのではなく使用されている また、このエラーは、私は、人々はそれが便利な私を助けるために見つけるかもしれないと思うのノートに付属していますキャラクター、これを覚えておいてください)。

答えて

1

あなたは(あなたがそれを出力ストリームの代わりに、入力ストリームを宣言する必要がifstream foutputの代わりofstream foutputを宣言している。

  1. ために演算子の優先順位のエラーを取り除くためにfoutput << (Str[i] += 1)foutput << Str[i] += 1を変更、またofstream foutput("out.txt");
  2. ifstream foutput("out.txt");を交換してください
1
ifstream foutput("out.txt"); 

Tハットは入力ストリームであり、出力ストリームではありません。出力ストリームを取得するには、std::ofstreamに変更します。


あなたはその後、ここに別のエラーを取得します:

foutput << Str[i] += 1; 

ために演算子の優先順位のです。かっこを挿入して修正してください。

foutput << (Str[i] += 1); 
関連する問題