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

问题描述 投票:1回答:2

我正在尝试制作一个简单的程序,将值修改为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();
}
c++ file fstream
2个回答
0
投票

这是getline错误吗?

./main.cpp:41:36:错误:调用'getline(std :: ofstream&,std :: __ cxx11 :: string&)'时没有匹配函数(getline(journal,line)

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

如果您首先专注于实现解密或加密,那么您将更容易解决编译问题。

您可以尝试注释掉encrypt_diary(string fileName,string line)函数,然后尝试再次编译并运行该程序。

我想你会发现你的decrypt_diary(string fileName,string line)函数运行正常。


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;
}

请注意,我们避免使用>><<,因为它们被格式化,默认情况下跳过空格(尽管你可以改变它)等。我们使用readwrite,因为它们只是存储字节,无论这些字节是什么。

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

#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替换d会导致它解密。

我们在这里没有使用字符串,因为我们并不真正需要它们,但如果你喜欢字符串到字符数组,你总是可以这样做。

这当然是一个相当简单的例子,您可以随意修改它并使其复杂化。还要注意,它可能不是处理此类问题的唯一(也可能是最好的)方法。

© www.soinside.com 2019 - 2024. All rights reserved.