【问题标题】:File Failure On Open打开文件失败
【发布时间】:2016-03-06 06:29:03
【问题描述】:

出于教育目的,我正在编写一个小型加密程序(不是真正的加密)。我已经对程序进行了重组,所以我的所有代码都在 main 中,以便为自己简化事情。

我的未加密文件失败。我不知道为什么。这是我目前所知道的。

  1. 使用 Xcode
  2. 通过切换文件打开(和测试)的顺序来验证未加密文件失败的测试语句:它是
  3. 转到构建阶段 >> 复制文件 >> 添加文件,将两个文件添加到绝对路径
  4. 我已经检查了文件的拼写和我的代码
  5. 再次更改顺序,但未加密文件仍然失败
  6. 清除了所有可能意外出现的标志
  7. 倒回文件的读/写位置

这是我的完整代码。

//  This program reads the contents of a file, encrypts it, and write the contents into a separate file.


#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <cmath>

using namespace std;

// Global Constants
const int POSITIVE_INT_LIMIT = 10;
const int NEGATIVE_INT_LIMIT = -10;
const int SLOPE_NEGATIVE = -1;
const int SLOPE_POSITIVE = 1;
const int STEP = 1;

int main() {

    int amplitude = 0, slope = SLOPE_NEGATIVE; //Set initial amplitude and slope.

    int streamSize = 1;

    char ch, cy;
    char *chPtr = &cy; // Initialize and assign char pointer.

    //open an unencrypted file to read from
    fstream unEncryptedFile;
    unEncryptedFile.open("testA", ios::in | ios::binary);

    //open a file to write encrypted info
    fstream cryptFile;
    cryptFile.open("testB", ios::out | ios::binary);

    //Clear flags previous set, just in case
    unEncryptedFile.clear();
    cryptFile.clear();

    //Rewind the files, just in case
    unEncryptedFile.seekg(0L, ios::beg);
    cryptFile.seekp(0L, ios::beg);

    if (unEncryptedFile.fail()) {
        cout << "Error opening read file." << endl;
        exit(1);
    }

    if (cryptFile.fail()) {
        cout << "Error opening write file." << endl;
        exit(1);
    }



    /*      Encryption pattern inside while-loop.

     limit>     10                             *
                9                             * *
                8                            *   *
                7                           *     *
                6                          *       * 
                5                         *         *
                4                        *           *
                3                       *             *
                2                      *               *
                1                     *                 *
     start>     0*2345678901234567890*2345678901234567890* -- < one hertz (cycle)
               -1 *                 *
               -2  *               *   (Number line: each integer represents a single while-loop cycle.)
               -3   *             *
               -4    *           *
               -5     *         *
               -6      *       *
               -7       *     *
               -8        *   *
               -9         * *
     limit>    -10         *

     */
    /*************************************************************
     The pattern above depictes a single character
     being read, and then the value of amplitude is added to it.

     *************************************************************/

    while (!unEncryptedFile.fail()) {

        ch = unEncryptedFile.get(); // Get the next character in the file.

        cout << ch << endl; // test for proper read

        if (amplitude > NEGATIVE_INT_LIMIT && slope == SLOPE_NEGATIVE) {
            amplitude -= STEP;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.

        } else if (amplitude <= NEGATIVE_INT_LIMIT){
            slope = SLOPE_POSITIVE;
            amplitude = NEGATIVE_INT_LIMIT;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.

        } else if (amplitude < POSITIVE_INT_LIMIT && SLOPE_POSITIVE){
            amplitude += STEP;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.

        } else if (amplitude >= POSITIVE_INT_LIMIT){
            slope = SLOPE_NEGATIVE;
            amplitude = POSITIVE_INT_LIMIT;
            cy = ch + amplitude;
            cryptFile.write(chPtr, streamSize); //Adjusted character value, cy, is written to file.
        }

    }

    //Files are closed.
    unEncryptedFile.close();
    cryptFile.close();



    return 0;
}

【问题讨论】:

  • 考虑 [stackoverflow.com/questions/24097580/… 作为失败检查最佳实践,并使用 while (!unEncryptedFile.eof()) 作为文件结束检查
  • @ankhzet,有趣的是,使用while (!unEncryptedFile.eof()) 创建了一个无限循环,输出为 \377...我不知道该怎么做。
  • 你试过unEncryptedFile.read(&amp;ch, 1);而不是ch = unEncryptedFile.get();while (!unEncryptedFile.eof())一起使用吗?
  • @ankhzet,我刚试过。结果相同。但有趣的是,\377 和 \376 一直显示为 ch 和 cy 的值。这些是转义字符,意思是“eof”。也许,由于某种原因,在阅读任何文本之前有一个 eof 转义字符?
  • @ankhzet, here 是关于 \377 的帖子 - 尽管它没有解释为什么我的阅读位置在世界上从 eof 开始,即使在明确将其放在文件开头之后也是如此

标签: c++ file encryption file-handling


【解决方案1】:

您需要在打开文件时提供文件扩展名和文件名

int main() {

    int amplitude = 0, slope = SLOPE_NEGATIVE; //Set initial amplitude and slope.

    int streamSize = 1;

    char ch, cy;
    char *chPtr = &cy; // Initialize and assign char pointer.

    //open an unencrypted file to read from
    fstream unEncryptedFile;

    unEncryptedFile.open("testA.txt", ios::in | ios::binary);

我用这个试过你的代码,它工作正常。

【讨论】:

  • 我刚刚尝试了扩展,但它对我不起作用...重新启动编译器...并且它没有无法打开...所以**我的编译器出了点问题** ://
猜你喜欢
  • 1970-01-01
  • 2012-05-23
  • 1970-01-01
  • 1970-01-01
  • 2012-06-02
  • 2011-03-29
  • 2017-03-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多