C ++文件帮助,加密和解密字符串

时间:2018-09-10 08:11:18

标签: c++ file fstream

我正在尝试制作一个简单的程序,将其值修改3(加密功能)->我希望它直接影响文件。但是我不确定如何。加密后,我将调用Decrypt函数(该函数完全按照我想要的方式工作),这样,您将必须运行程序以访问日记。无论如何,我可以像在解密功能中一样修改单个字符吗?我不断收到一条getline错误。

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void decrypt_diary(string,string);
void encrpyt_diart(string,string);

int main()
{
    string fileName = "Diary.txt";
    string line;
    decrypt_diary(fileName,line);
    return 0;
}
void decrypt_diary(string fileName, string line)
{
    ifstream journal;
    journal.open(fileName);
    if (journal.is_open())
    {
        while (getline(journal,line))
        {
            for (int i=0; i<signed(line.length()); i++)
            {
                line[i] = line[i] + 3;
            }
            cout << line << endl;
        }
    }
    journal.close();
}

void encrypt_diary(string fileName, string line)
{
    ofstream journal;
    journal.open(fileName);
    if (journal.is_open())
    {
        while (getline(journal,line))
        {
            for (int i=0; i<signed(line.length()); i++)
            {
                line[i] = line[i] - 3;
//              journal << line[i];
            }
            cout << line << endl;
        }
    }
    journal.close();
}

2 个答案:

答案 0 :(得分:0)

这是您的意思是getline错误吗?

  

./ main.cpp:41:36:错误:没有匹配的函数可以调用“ getline(std :: ofstream&,std :: __ cxx11 :: string&)”            而(getline(journal,line)

如果是这样,我认为问题在于没有 getline()函数采用 ofstream 对象作为 getline()用于读取文件,而 ofstream(输出文件流)用于写入文件。在 decrypt_diary 函数中它可以正常工作,因为它需要一个 ifstream(输入文件流)

如果您专注于首先实现解密或加密,则可以轻松解决编译问题。

您可以尝试注释掉 encrypt_diary(字符串文件名,字符串行)函数,然后尝试编译并再次运行该程序。

我想您会发现您的 decrypt_diary(字符串fileName,字符串行)功能正常。

答案 1 :(得分:0)

因此,如果我正确理解了您想要的内容,则希望逐个字符地浏览文件,然后将其替换为其他字符。我将再次指出,逐个字符并不是最好的主意,但是对块的修改应该足够简单。

#include <string>
#include <fstream>
#include <iostream>

inline void encrypt_char(char& in) {
    in += 3;
}

inline void decrypt_char(char& in) {
    in -= 3;
}

int main(int argc, char** argv) {
    //These first 2 lines just process the command line argument
    if (argc < 2 || (argv[1][0] != 'e' && argv[1][0] != 'd') || argv[1][1] != '\0') return 1;
    bool encrypt_mode = (argv[1][0] == 'e' ? true : false);
    std::fstream file("Diary.txt", std::ios::in | std::ios::out);
    file.seekg(0, std::ios::end);
    size_t size = file.tellg(); //This should tell us the file size
    char c;
    for (size_t i = 0; i < size; i++) {
        file.seekg(i, std::ios::beg); //Set read position
        file.read(&c, 1); //Read a character
        if (encrypt_mode) encrypt_char(c);
        else decrypt_char(c);
        file.seekp(i, std::ios::beg); //Set write position
        file.write(&c, 1); //Write a character
    }
    file.close();
    return 0;
}

请注意,我们避免使用>><<格式化的格式,默认情况下跳过空格(尽管您可以更改),等等。相反,我们使用read和{{1} },因为它们只是存储字节,无论这些字节是多少。

这种方法意味着我们会对文件进行大量操作,并且可能不是最快的。相反,您可以分块读取,这种修改非常简单,只需将字符替换为字符数组,一次读取一个以上的字节即可。但是,如果可能的话,一次读取整个文件可能是最简单的。

write

使用#include <string> #include <fstream> #include <iostream> inline void encrypt_char(char& in) { in += 3; } inline void decrypt_char(char& in) { in -= 3; } int main(int argc, char** argv) { //These first 2 lines just process the command line argument if (argc < 2 || (argv[1][0] != 'e' && argv[1][0] != 'd') || argv[1][1] != '\0') return 1; bool encrypt_mode = (argv[1][0] == 'e' ? true : false); std::fstream file("Diary.txt", std::ios::in | std::ios::out); file.seekg(0, std::ios::end); size_t size = file.tellg(); //This should tell us the file size file.seekg(0, std::ios::beg); char* fileData = new char[size]; //Don't need null-termination file.read(fileData, size); //Read the whole file for (size_t i = 0; i < size; i++) { //Process the characters if (encrypt_mode) encrypt_char(fileData[i]); else decrypt_char(fileData[i]); } file.seekp(0, std::ios::beg); //Find the start of the file file.write(fileData, size); //Write the whole thing delete[] fileData; file.close(); return 0; } 调用程序会对其进行加密,将./myProgram e替换为e会导致其解密。

我们在这里没有使用字符串,因为我们并不是真的需要它们,但是如果您更喜欢字符串而不是字符数组,则可以始终这样做。

这当然是一个非常简单的示例,您可以根据需要随意对其进行修改和使其复杂化。还要注意,这可能不是解决此类问题的唯一(也是最好的)方法。

相关问题