2017-04-16 18 views
0

그래서 파일에서 문자를 읽고 문자 배열에 저장 한 다음 새로운 파일. 단, 모든 문자의 원시 숫자가 하나씩 증가합니다 (대부분의 숙련 된 프로그래머는 여기서 내가 아는 내용을 알게됩니다). 그래서 기본적으로 내 자신의 암호화 알고리즘을 만들려고합니다. 그러나 나는 매우 이상한 오류가 발생합니다 : <various paths here>\main.cpp|27|error: no match for 'operator<<' (operand types are 'std::ifstream {aka std::basic_ifstream<char>}' and 'char')| 이 오류에 대해 많이 들었습니다.하지만 사람들이 클래스 정의 함수를 사용할 때만 발생합니다. 프로그램에서하지 않은 것입니다. 이 오류가 나는 사람들이 찾을 수있는 생각 메모와 함께 그것을 유용하게 저를 도와 :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; 
} 

주 : I 출력 문자열에 fstream를 사용하고있다 (안 <various paths here>\main.cpp|27|note: no known conversion for argument 1 from 'std::ifstream {aka std::basic_ifstream<char>}' to 'int'| 소스 코드는 다음과 같다 문자들, 이것을 염두에 두라.) 다른 시간에 파일에, 그리고 그것은 잘 작동했다! 대신 ofstream foutputifstream foutput을 선언 한

답변

1

(대신 입력 스트림의 그것을 출력 스트림을 선언해야합니다.

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