【问题标题】:ofstream error流错误
【发布时间】:2009-03-07 10:55:17
【问题描述】:

这是对我提出的另一个问题的参考,尽管它完全是它自己的问题。

编译时出现两个错误:

1>.\asst4.cpp(73) : 错误 C2065: 'outfile' : 未声明的标识符

1>.\asst4.cpp(73) : 错误 C2228: '.close' 左侧必须有类/结构/联合

我对我在这里做错了什么感到有些困惑?有什么建议或想法吗? (实际的输出文件靠近代码的顶部。

这里是完整的代码:

#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.
    }
}

提前致谢! 杰夫

【问题讨论】:

    标签: c++ ofstream


    【解决方案1】:

    范围问题。您在 while 循环中声明 outfile,但试图在所述 while 循环之外访问它。

    ofstream outfile("encrypted.txt", ios::out); 移动到ifstream infile(filename.c_str()); 之后的行,即while(!infile.eof()) 之前的行。

    【讨论】:

    • 做到了,现在我的逻辑存在一个实际缺陷,在某个地方决定不打印到文件中......而不久前在运行它时,当我停止它时,因为它看起来冻结并打开txt文件,我居然设法让记事本崩溃,指点我!
    【解决方案2】:

    您有两个选择 - 正如 X-istence 所提到的,未编译您对 outfile.close() 的调用的原因是因为对象“outfile”与 outfile 的声明不存在于同一范围内。

    你可以移动这条线

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

    在 while 循环之外,因此它遵守与 infile 相同的范围规则,或者您可以将调用移至 outfile.close();在while循环中,这会将其移动到outfile所在的当前范围内。

    我的偏好是将 outfile 的声明移到 while 循环之外,因为打开文件是一项非常昂贵的操作,而且您真的不想对从 infile 读取的每个字母都执行此操作。在这种情况下,打开一次,关闭一次是要走的路。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 2021-06-13
      • 2021-08-28
      • 2018-06-21
      • 2015-08-19
      • 2016-07-16
      • 2011-12-08
      相关资源
      最近更新 更多