从文件 c++ 将单个字符读入变量

Reading single char's into variables from file c++

本文关键字:变量 字符 单个 文件 c++      更新时间:2023-10-16

我正在处理文件输入/输出,我正在尝试制作一个编码/解码程序。我需要帮助将文件中的编码字符读取回要解码的程序中。(这不是一项作业,因为我明年才上九年级,我正努力做到这一点,因为这似乎很有挑战性。)这是我的代码:

#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函数以使用这些映射。