从文件 c++ 将单个字符读入变量
Reading single char's into variables from file c++
我正在处理文件输入/输出,我正在尝试制作一个编码/解码程序。我需要帮助将文件中的编码字符读取回要解码的程序中。(这不是一项作业,因为我明年才上九年级,我正努力做到这一点,因为这似乎很有挑战性。)这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>
/*
* OUT: categorize words into char's, assign a symbol/number to each char, output number
* combonation to file.
IN: load file, decode file, read decoded version. */
using namespace std;
void Encode(){
char message[100];
char ENCODED[100];
cout<<"input new content:n>";
cin.getline(message, 99);
cin.ignore();
cout<<"encoding...n";
for (int i=0; i<100; ++i){
if (message[i]=='a') ENCODED[i]='1';
else if (message[i]=='b') ENCODED[i]='$';
else if (message[i]=='c') ENCODED[i]='!';
else if (message[i]=='d') ENCODED[i]='*';
else if (message[i]=='e') ENCODED[i]='2';
else if (message[i]=='f') ENCODED[i]='&';
else if (message[i]=='g') ENCODED[i]='^';
else if (message[i]=='h') ENCODED[i]='%';
else if (message[i]=='i') ENCODED[i]='3';
else if (message[i]=='j') ENCODED[i]='=';
else if (message[i]=='k') ENCODED[i]='_';
else if (message[i]=='l') ENCODED[i]='-';
else if (message[i]=='m') ENCODED[i]='2';
else if (message[i]=='n') ENCODED[i]='9';
else if (message[i]=='o') ENCODED[i]='4';
else if (message[i]=='p') ENCODED[i]='|';
else if (message[i]=='q') ENCODED[i]='/';
else if (message[i]=='r') ENCODED[i]='>';
else if (message[i]=='s') ENCODED[i]='?';
else if (message[i]=='t') ENCODED[i]='}';
else if (message[i]=='u') ENCODED[i]='5';
else if (message[i]=='v') ENCODED[i]=',';
else if (message[i]=='w') ENCODED[i]='.';
else if (message[i]=='x') ENCODED[i]=';';
else if (message[i]=='y') ENCODED[i]=')';
else if (message[i]=='z') ENCODED[i]='@';
else if (message[i]==' ') ENCODED[i]='#';
else if (message[i] ==' ') {ENCODED[i] = '}'; break;}
else ENCODED[i]=' ';
}
cout<<"done encoding.n";
cout<<"exporting file...n";
ofstream OUTfile ("encoded.txt");
OUTfile<<ENCODED;
cout<<"file exported to parent directory.n";
cin.get();
}
void Decode(){ //this is where I run into problems!!
string encoded[100]
char DECODED[100]
ifstream INfile ("encoded.txt");
cout<<"Decoding...n";
INfile>>encoded;
for (i=0; i<100; ++i){
if (encoded[i]=='1') DECODED[i]='a';
else if (encoded[i]=='$') DECODED[i]='b';
else if (encoded[i]=='!') DECODED[i]='c';
else if (encoded[i]=='*') DECODED[i]='d';
else if (encoded[i]=='2') DECODED[i]='e';
else if (encoded[i]=='&') DECODED[i]='f';
else if (encoded[i]=='^') DECODED[i]='g';
else if (encoded[i]=='%') DECODED[i]='h';
else if (encoded[i]=='3') DECODED[i]='i';
else if (encoded[i]=='=') DECODED[i]='j';
else if (encoded[i]=='_') DECODED[i]='k';
else if (encoded[i]=='-') DECODED[i]='l';
else if (encoded[i]=='2') DECODED[i]='m';
else if (encoded[i]=='9') DECODED[i]='n';
else if (encoded[i]=='4') DECODED[i]='o';
else if (encoded[i]=='|') DECODED[i]='p';
else if (encoded[i]=='/') DECODED[i]='q';
else if (encoded[i]=='>') DECODED[i]='r';
else if (encoded[i]=='?') DECODED[i]='s';
else if (encoded[i]=='}') DECODED[i]='t';
else if (encoded[i]=='5') DECODED[i]='u';
else if (encoded[i]==',') DECODED[i]='v';
else if (encoded[i]=='.') DECODED[i]='w';
else if (encoded[i]==';') DECODED[i]='x';
else if (encoded[i]==')') DECODED[i]='y';
else if (encoded[i]=='@') DECODED[i]='z';
else if (encoded[i]=='#') DECODED[i]=' ';
else if (encoded[i] ==' ') {DECODED[i] = '}'; break;}
else DECODED[i]==' ';
}
cout<<"Decoded file content: "<<DECODED;
cin.get();
}
int main(){
string choice;
cout<<"Encode new message or decode previous file?n> ";
cin>>choice;
cin.ignore();
if (choice=="encode") Encode();
if (choice=="decode") Decode();
return 0;
}
正如你所看到的,当涉及到解码函数时,我不知道我在做什么。任何帮助都将不胜感激!非常感谢。
编辑:我用提供的建议更新了代码,但当编译器到达"INfile>>编码;"这一行时,它说在"INfile>>编码"中没有匹配运算符">>"。。。
问题:
编译失败(g++-4.5.1),未找到合适的ifstream
构造函数重载,需要name.c_str()
。
ifstream INfile (name);
在接下来的两个声明中缺少分号
char encoded[100]
char DECODED[100]
循环计数器i
未声明(for(int i = 0; ...
)
for (i=0; i<100; ++i){
if (encoded[i]=='1') DECODED[i]='a';
从现在开始,您将DECODED[i]
与字符常量进行比较,而不是分配它们。用DECODED[i] =
替换所有这些DECODED[i]==
。
else if (encoded[i]=='$') DECODED[i]=='b';
else if (encoded[i]=='!') DECODED[i]=='c';
else if (encoded[i]=='*') DECODED[i]=='d';
另一个问题是,无论实际消息是否较短,都要对100个字符进行编码和解码。在Decode()
和Encode()
中,添加对字符串末尾的检查
if (array[i] ==' ') {
other_array[i] = '}';
break;
}
以结束转换。
我认为这里的问题是您不理解char和string之间的区别。字符是一个字母或符号,如"A"、"b"、"c"、"1"、"0"、"^"(等等)。但字符串是一个字符序列,如"abc"、"123"、"*&^"等。在C/C++中,为了指定字符,我们使用单引号,如"a"。单引号内的值只能包含一个字母。为了指定字符串,我们使用双引号,就像前面提到的一样。双引号内的值可以包含任意数量的字符。
由于这是您自己的练习,我可以建议您创建一个映射来避免那些疯狂的if语句吗(并使以后更容易修改您的编码):
// Warning: always address these with ints or unsigned char, not char
// if you expect extended ASCII characters
static unsigned char encode_map[256] = {0};
static unsigned char decode_map[256] = {0};
void InitEncodingMap()
{
encode_map['a'] = '1';
encode_map['b'] = '$';
encode_map['c'] = '!';
encode_map['d'] = '1';
// etc...
encode_map['z'] = '@';
encode_map[' '] = '#';
// Create the reverse map
for( int i = 0; i < 256; i++ ) {
unsigned char encoded = encode_map[i];
if( decode_map[encoded] != 0 ) {
printf("Collision for mapping %c -> %cn", (char)i, encoded );
} else {
decode_map[encode_map[i]] = (unsigned char)i;
}
}
}
现在,请尝试重写Encode和Decode函数以使用这些映射。
- 如何在保持其值的同时将 c++ 无符号字符变量转换为 char 变量
- 如何使用C++将字符串中的字符转换为整数变量
- 如何从字符串中找到字符(变量)?
- C++理解计算字符变量的问题
- 变量名称中的北欧字符C++
- 将十六进制值设置为用于填充的字符变量时出现问题
- 将字符数组转换为结构时出现问题. 结构的字符数组变量溢出
- 使用单个变量对字符输入进行计数
- 将子字符串字符从字符串值转换为 int,然后将其分配给 int 变量
- 将字符串变量分配给无符号字符变量
- 为什么我无法获取 MSVS2019 / C++ 中字符或uint8_t变量的内存地址?
- 将 static_cast<int>(-15) 分配给静态常量字符类型变量
- 在c++中读取文件时,它如何自动将字符放入字符数组,将数字放入整数变量
- 当对字符变量使用toupper()时,所述char变量输出多个字符.我该如何防止这种情况发生
- 如何从字符数组C++中提取2个整数并将它们存储在2个变量中(初学者)
- 如何将字符类型变量附加到字符串中?
- C++ 无法将字符数组变量分配给字符串变量
- C++无法打印字符变量
- 如何查看两个字符变量是否相等
- 双变量字符变量