打开文件失败 [英] File Failure On Open
本文介绍了打开文件失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了教育目的,我正在编写一个小型加密程序(不是真正的加密)。我重组了程序,所以我的代码主要是为了简化自己的事情。
for educational purposes, I am writing a small encryption program (not real encryption). I've restructured the program so all my code is in main to simplify things for myself.
我的加密文件失败。我不知道为什么这是我迄今为止所知道的。
My unEncrypted file fails. I don't know why. Here's what I know so far.
- 使用Xcode
- 通过切换文件被打开(并测试)以仅验证未加密的文件失败:它是
- 没有建立阶段>>复制文件>>添加文件,将这两个文件添加到绝对路径
- 我已经检查了文件的拼写和我的代码
- 更改了订单,但仍然未加密的文件失败
- 清除任何可能偶然发生的旗帜
- 重写文件的读/写位置
- Using Xcode
- Tested statements by switching the order which the files are opened (and tested) to verify only the unencrypted file failing: it is
- Gone to Build Phases >> Copy Files >> Add File, adding both files to Absolute Path
- I've checked the spelling of the files and my code
- Changed the order again for good measure, still the unencrypted file fails
- Cleared any flags which may accidentally be there
- Rewound the read/write positions of the files
这是我的代码全部。
// 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;
}
推荐答案
您需要提供文件扩展名与打开文件时的文件名
you need to provide the file extension with the file name while opening a file
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);
我尝试过你的代码,它的工作。
I tried your code with this and it's working.
这篇关于打开文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文