2009-03-07 3 views
0

이것은 다른 질문에 대한 참조입니다. >ofstream 오류

1 \ asst4.cpp (73) :

은 내가 두 가지 오류를 얻을 컴파일 할 때. 오류 C2065 : 'OUTFILE'. 선언되지 않은 식별자

1> \ asst4.cpp (73) : 오류 C2228 : '.close'의 왼쪽에/struct/union 클래스가 있어야합니다.

여기서 잘못 수행 한 것에 대해 약간 혼란 스럽습니까? 모든 추천이나 아이디어? (실제 OUTFILE는 코드의 상단입니다 여기에

전체 코드는 다음과 같습니다.!

#include<iostream> 
#include<fstream> //used for reading/writing to files. 
#include<string> //needed for the filename. 
#include<stdio.h> //for goto statement 

using namespace std; 

int main() 
{ 
    string start; 
    char choice; 
    char letter; 
    int x; 
    int y; 
    int z; 
    string filename; 
    int garbage = rand()%('!' - '~' + 1); 
    cout << "Would you like to encrypt or decrypt a file? Please type enc, dec, or stop (case sensitive): " ; 
    cin >> start; 
    while(start == "enc") 
    { 
     x = 1; 
     y = 1; 
     cout << "How many garbage characters would you like between each correct character?: " ; 
     cin >> z; 
     cout << endl << "Please insert the name of the document you wish to encrypt, make sure you enter the name, and the file type (ie: filename.txt): " ; 
     cin >> filename; 
     ifstream infile(filename.c_str()); 
     while(!infile.eof()) 
     { 
      ofstream outfile("encrypted.txt", ios::out); 
      infile.get(letter); 
      if (x == y)   
       { 
       outfile << garbage; 
       x++;    
      } 
      else 
      { 
       if((x - y) == z)    
       { 
        outfile << letter;   
        y = x;     
       } 
       else       
       {       
        outfile << garbage; 
        x++; 
       } 
      } 
     } 
     cout << endl << "Encryption complete...please return to directory of program, a new file named encrypted.txt will be there." << endl; 
     infile.close(); 
     outfile.close(); 
     cout << "Do you wish to try again? Please press y then enter if yes (case sensitive)."; 
     cin >> choice; 
     if(choice == 'y') 
     { 
      start = "enc"; 
     } 
     else 
     { 
      cout << endl << "Do you wish to decrypt a file? Please press y then enter if yes (case sensitive)."; 
      if(choice = 'y') 
      { 
       start == "dec"; 
      } 
      else 
      { 
       start == "no"; 
      } 
     } 
    } 
    while(start == "dec") 
    { 

     //lets user choose whether to do another document or not. 
     //used to track each character in the document. 
     x = 1; //first counter for tracking correct letter. 
     y = 1; //second counter (1 is used instead of 0 for ease of reading, 1 being the "first character"). 
       //third counter (used as a check to see if the first two counters are equal). 
     //allows for user to input the filename they wish to use. 
     cout << "Please make sure the document is in the same file as the program, thank you!" << endl << "Please input document name: " ; 
     cin >> filename; //getline(cin, filename); 
     cout << endl; 
     cout << "'Every nth character is good', what number is n?: "; 
     cin >> z; //user inputs the number at which the character is good. IE: every 5th character is good, they would input 5. 
     cout << endl; 
     z = z - 1; //by subtracting 1, you now have the number of characters you will be skipping, the one after those is the letter you want. 
     ifstream infile(filename.c_str()); //gets the filename provided, see below for incorrect input. 
     if(infile.is_open()) //checks to see if the file is opened. 
     { 
      while(!infile.eof()) //continues looping until the end of the file. 
      { 
        infile.get(letter); //gets the letters in the order that that they are in the file. 
        if (x == y)   //checks to see if the counters match... 
        { 
         x++;    //...if they do, adds 1 to the x counter. 
        } 
        else 
        { 
         if((x - y) == z)   //for every nth character that is good, x - y = nth - 1. 
         { 
          cout << letter;   //...if they don't, that means that character is one you want, so it prints that character. 
          y = x;     //sets both counters equal to restart the process of counting. 
         } 
         else      //only used when more than every other letter is garbage, continues adding 1 to the first 
         {       //counter until the first and second counters are equal. 
          x++; 
         } 
        } 
      } 
      cout << endl << "Decryption complete...please return to directory of program, a new file named encrypted.txt will be there." << endl; 
      infile.close(); 
      cout << "Do you wish to try again? Please press y then enter if yes (case sensitive)."; 
      cin >> choice; 
      if(choice == 'y') 
      { 
       start == "dec"; 
      } 
      else 
       { 
       cout << endl << "Do you wish to encrypt a file? Please press y then enter if yes (case sensitive)."; 
       if(choice == 'y') 
       { 
        start == "enc"; 
       } 
       else 
       { 
        start == "no"; 
       } 
      } 
     } 
     else //this prints out and program is skipped in case an incorrect file name is used. 
     { 
      cout << "Unable to open file, please make sure the filename is correct and that you typed in the extension" << endl; 
      cout << "IE:" << "  filename.txt" << endl; 
      cout << "You input: " << filename << endl; 
      cout << "Do you wish to try again? Please press y then enter if yes (case senstive)." ; 
      cin >> choice; 
      if(choice == 'y') 
      { 
       start == "dec"; 
      } 
      else 
      { 
       start == "no"; 
      } 
     } 
     getchar(); //because I use visual C++ express. 
    } 
} 

감사합니다 사전에 제프

당신은 당신의 동안 내 OUTFILE 선포

답변

2

범위 지정 문제를 해결합니다. 루프, 아직 액세스하려고하는 외부의 말했다 while 루프.

당신의 while(!infile.eof()) 앞에있는 라인으로 이동 ofstream outfile("encrypted.txt", ios::out); 바로 ifstream infile(filename.c_str()); 후.

+0

그런데 논리에 실제 결함이 있습니다. 어딘가에 파일에 인쇄하지 않기로 결정했습니다. 그리고 잠시 전에 그것을 실행하면서 멈추었을 때 얼어 붙은 것처럼 보였습니다. txt 파일, 나는 그럭저럭 충돌 할 메모장을 실제로 얻을 수 있었다, 나를 위해 점을 지적해라! – Jeff

+0

언제나처럼, 도움을 주셔서 감사합니다! – Jeff

0

당신은 outfile.close()에 대한 호출이 outfile 선언과 같은 범위에 존재하지 않기 때문에 'outfile'객체가 존재하지 않기 때문에 X-istence에 언급 된 것처럼 두 가지 선택 사항이 있습니다.

) (이 INFILE과 같은 범위 지정 규칙을 따르는 있도록 while 루프 외부 라인


ofstream outfile("encrypted.txt", ios::out); 

을 움직일 수 중 하나, 또는 당신은 outfile.close하도록 통화를 이동할 수 있습니다; while 루프 내부에서 outfile이있는 현재 범위로 이동합니다.

필자가 선호하는 것은 while 루프 외부에서 outfile 선언을 이동하는 것입니다. 파일을 여는 것은 상당히 비싼 작업이므로 infile에서 읽은 모든 문자에 대해이 작업을 수행하지 않으려 고합니다. 한 번 열어이 케이스를 닫으면 한 번 닫습니다.