将变量存储在C++; 问题是文件不会被读取…显然数组有问题,但我不知道如何修复这个问题…我是C++的初学者和“字符串”……< /P>初学者。
我的文件应该读取代码,然后翻译文件,然后将文本输出到新文件中将变量存储在C++; 问题是文件不会被读取…显然数组有问题,但我不知道如何修复这个问题…我是C++的初学者和“字符串”……< /P>初学者。,c++,arrays,sorting,file-io,encryption,C++,Arrays,Sorting,File Io,Encryption,我的文件应该读取代码,然后翻译文件,然后将文本输出到新文件中 #include <iostream> #include <iomanip> #include <string> #include <sstream> #include <fstream> #include <math.h> #include <stdio.h> #include <string> #include <string.h
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
int main()
{
// Declarations
string reply;
string inputFileName;
ifstream inputFile;
ofstream outFile;
char character;
cout << "Input file name: ";
getline(cin, inputFileName);
// Open the input file.
inputFile.open(inputFileName.c_str());
// Check the file opened successfully.
if ( ! inputFile.is_open()) {
cout << "Unable to open input file." << endl;
cout << "Press enter to continue...";
getline(cin, reply);
return 1;
}
// This section reads and echo's the file one character (byte) at a time.
while (inputFile.peek() != EOF) {
inputFile.get(character);
//cout << character;
//Don't display the file...
char cipher[sizeof(character)];
//Caesar Cipher code...
int shift;
do {
cout << "enter a value between 1-26 to encrypt the text: ";
cin >> shift;
}
while ((shift <1) || (shift >26));
int size = strlen(character);
int i=0;
for(i=0; i<size; i++)
{
cipher[i] = character[i];
if (islower(cipher[i])) {
cipher[i] = (cipher[i]-'a'+shift)%26+'a';
}
else if (isupper(cipher[i])) {
cipher[i] = (cipher[i]-'A'+shift)%26+'A';
}
}
cipher[size] = '\0';
cout << cipher << endl;
}
cout << "\nEnd of file reached\n" << endl;
// Close the input file stream
inputFile.close();
cout << "Press enter to continue...";
getline(cin, reply);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
//声明
字符串回复;
字符串输入文件名;
ifstream输入文件;
出流孔的直径;
字符;
cout您使用的是单个字符,例如,只有一个字母或一个数字。因此,整个大小处理都是无用的,因为大小总是1。您可能应该使用const char*。但是,您根本不能使用filestream.get(),因为它只返回单个字符(而不是和cstring aka const char*)。
您可以使用fstream.get()作为循环的条件,因此不需要请求eof标志
char my_char;
std::ifstream infstream("filename.txt");
if(!infstream.isopen())
return -1;
while(infstream.get(my_char) {
//do some stuff
}
或
< C++中的动态数组使用STD::vector或STD::……或……其他STL容器之一,这样就不必浪费时间在内存管理和使用静态大小数组。
STD::字符串是C++中字符串的方法。它类似于STL容器,但仅针对char的 ,请简短:你在C++上,所以不要使用C的全部东西。
- 不要使用字符数组,请使用
std::string
- 不要使用
islower(char)
而是使用std::islower(char,locale)
- 不要使用C样式数组,而是使用
std::array
(编译时常量大小)或std::vector
(动态大小)
您希望它更像这样:
#include <string>
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <locale>
int main (void)
{
std::string input_filename;
std::cout << "Input file name: ";
std::getline(std::cin, input_filename);
unsigned int shift;
do
{
std::cout << "Enter a value between 1-26 to encrypt the text: ";
std::cin >> shift;
}
while ((shift == 0) || (shift > 26));
try
{
std::string filestring;
std::ifstream input(input_filename, std::ios_base::in);
if (input)
{
input.seekg(0, std::ios::end);
filestring.reserve(input.tellg());
input.seekg(0, std::ios::beg);
filestring.assign
(std::istreambuf_iterator<char>(input),
std::istreambuf_iterator<char>());
}
else
{
std::string error_string("Reading failed for: \"");
error_string.append(input_filename);
error_string.append("\"");
throw std::runtime_error(error_string);
}
std::string result;
result.reserve(filestring.size());
std::locale const loc;
for (auto character : filestring)
{
char const shifter(std::islower(character, loc) ? 'a' : 'A');
result.push_back((character-shifter+shift)%26+shifter);
}
std::cout << result << std::endl;
}
catch (std::exception & e)
{
std::cout << "Execution failed with an exception: " << std::endl;
std::cout << e.what() << std::endl;
}
}
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
std::字符串输入\ u文件名;
std::cout移位;
}
而((shift==0)| |(shift>26));
尝试
{
std::string filestring;
std::ifstream输入(输入文件名,std::ios\u base::in);
如果(输入)
{
input.seekg(0,std::ios::end);
reserve(input.tellg());
input.seekg(0,std::ios::beg);
filestring.assign
(标准::istreambuf_迭代器(输入),
std::istreambuf_迭代器();
}
其他的
{
std::字符串错误\u字符串(“读取:\”“”失败);
错误\u string.append(输入\u文件名);
错误\u字符串。追加(\“”);
抛出std::运行时错误(错误字符串);
}
std::字符串结果;
result.reserve(filestring.size());
std::locale const loc;
for(自动字符:filestring)
{
字符常量移位器(std::islower(字符,loc)?'a':'a');
结果。将_向后推((字符移位器+移位器)%26+移位器);
}
std::cout通过查看代码,“character”被声明为char
,这意味着它只能存储一个字节的信息。但后来,您开始使用它,就好像它是一个字符数组一样
你也可以声明“密码”,就像你手工管理的字符数组一样,它是容易出错的。但是真正的问题是你在C++中混合了类似C的代码。换句话说,你编写的代码的方式不被认为是习惯的C++。
Pixelchemist已经讨论了要点,因此我将仅提供上述代码的一个最小重构工作示例:
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
string filename;
cout << "enter input file: ";
cin >> filename;
ifstream inputFile( filename.c_str() );
string plaintext;
do
{
plaintext += inputFile.get();
}while(inputFile);
cout << plaintext << endl;
string &ciphertext = plaintext;
//Caesar Cipher code...
int shift = rand() % 26 + 1;
for(size_t i = 0; i < ciphertext.size(); ++i)
{
if (islower(ciphertext[i])) {
ciphertext[i] = (ciphertext[i] - 'a' + shift) % 26 + 'a';
}
else if (isupper(ciphertext[i])) {
ciphertext[i] = (ciphertext[i] - 'A' + shift) % 26 + 'A';
}
}
cout << ciphertext << endl;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符串文件名;
cout>文件名;
ifstream输入文件(filename.c_str());
字符串明文;
做
{
纯文本+=inputFile.get();
}while(输入文件);
cout“显然数组存在问题,但我真的不知道如何解决此问题…”如果您收到错误消息,您应该提供给我们查看。您正在向strlen
传递一个字符,该字符需要const char*
类型。为什么要调用strlen?字符总是为1。您希望char cipher[sizeof(character)]
具体做什么?这段代码编译了吗?@greatwolf我希望从文件中读取“全文”,然后使用Caesar密码进行翻译…那么我该如何解决这个问题呢?这正是我试图解决的问题…我的意思是你告诉我删除一个部分以使另一个部分正常工作…但是我该如何删除filestream.get()?您能告诉我如何以及为什么使用“catch”和“throw”'final.cpp:42:15:error:'character'不命名类型''final.cpp:47:5:error:expected';'before'std''final.cpp:48:3:error:expected primary expression before'}'标记''final.cpp:48:3:error:expected')'before'}“token”“final.cpp:48:3:错误:应在“}”token“final之前使用主表达式。cpp:48:3:错误:应为“;”在“}”之前token@user2066703我使用try-and-catch,因为如果文件不存在、无法打开或流初始化时发生另一个错误,我会抛出std::runtime\u错误。我认为问题可能是基于范围的for循环。您是否启用了C++11?你使用哪种编译器?如果GCC比版本<代码> 4.6 < /C>要使用<代码> -STD= C++ 0x(或<代码> -STD= C++ 11 < /COD>自GCC <代码> 4.7代码> >。所以,只是一个快速问题。PixelCurristor……在编写代码时,可以结合C和C++吗?或者我应该只和一个在一起,永远不能组合…@ USE2066 703,你不能绕过C(例如用C语言写的IHBRARS),但是在使用C++时你应该坚持C++。您是将“cipher”声明为“数组”还是“字符串”?@user2066703看起来我错过了一个复制粘贴。哎呀!它现在已经固定了,非常感谢帮助……只是我是C++初学者,所以我不知道C和C++之间的区别,我想把密文输出到一个新文件中…我还可以使用**of stream.open(“transcripted.txt”)***@user2066703是的,
size_t const N(filestring.length());
for (size_t i(0u); i<N; ++i)
{
char const shifter(std::islower(filestring[i], loc) ? 'a' : 'A');
result.push_back((filestring[i]-shifter+shift)%26+shifter);
}
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
string filename;
cout << "enter input file: ";
cin >> filename;
ifstream inputFile( filename.c_str() );
string plaintext;
do
{
plaintext += inputFile.get();
}while(inputFile);
cout << plaintext << endl;
string &ciphertext = plaintext;
//Caesar Cipher code...
int shift = rand() % 26 + 1;
for(size_t i = 0; i < ciphertext.size(); ++i)
{
if (islower(ciphertext[i])) {
ciphertext[i] = (ciphertext[i] - 'a' + shift) % 26 + 'a';
}
else if (isupper(ciphertext[i])) {
ciphertext[i] = (ciphertext[i] - 'A' + shift) % 26 + 'A';
}
}
cout << ciphertext << endl;
}