从文件c ++中读取单个字符到变量中

时间:2012-08-01 00:54:05

标签: c++ file input io

我正在搞乱文件输入/输出,我正在尝试制作编码/解码程序。我需要帮助从文件中读取编码的字符回到要解码的程序中。 (这不是作业,因为我明年才进入9年级,我试图这样做,因为它看起来很有挑战性。) 这是我的代码:

#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] =='\0') {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] =='\0') {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&gt;&gt; encoded;”行时它说运营商“&gt;&gt;”没有匹配在“INfile&gt;&gt; encoded”......

3 个答案:

答案 0 :(得分:1)

我认为这里的问题是你不理解char和string之间的区别。 char是一个字母或符号,如'a''b''c''1''0''^'(等等但是一个字符串 - 它作为一系列字符,如“abc”“123”“* &amp; ^“等在C / C ++中指定一个char,我们使用单引号,如'a'。单引号内的值只能有一个字母。要指定一个字符串,我们使用双引号,如前所述。双引号内的值可以包含任意数量的字符。

答案 1 :(得分:1)

问题:

编译失败(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] =='\0') {
    other_array[i] = '\}';
    break;
}

结束转换。

答案 2 :(得分:0)

由于这是一个自己的练习,我可以建议你创建一个映射来避免那些疯狂的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 -> %c\n", (char)i, encoded );
        } else {
            decode_map[encode_map[i]] = (unsigned char)i;
        }
    }
}

现在,尝试重写您的编码和解码功能以使用这些地图。