gpt4 book ai didi

C++读取多个文本文件(Ubuntu)

转载 作者:行者123 更新时间:2023-12-04 19:24:16 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





can't use filesystem in g++ / msys2 on windows

(1 个回答)


15 天前关闭。




我有如下代码。简而言之,它通过移动 3 行来加密文本文件中的所有字母(Caesar Cipher)
我想出了如何加密单个文件,但我不知道如何加密多个文件。我不太了解 c++,但我需要为我的项目做这件事。通过研究,我能够走到这一步。有人可以告诉我如何读取多个文件并将加密集成到此代码中吗?

#include <iostream>
#include <fstream>
#include <string>
#include <filesystem>
using namespace std;

class Sezar
{
public:
void yaziOku(char *dosyaOku);
void sifrele(char *dosyaOku, char *dosyaYaz, int key);
void sifreCoz(char *dosyaOku, char *dosyaYaz, int key);
};

void Sezar::yaziOku(char *dosyaOku)
{
ifstream input;
char gecici;
input.open(dosyaOku);
cout << "\n\n <--- " << dosyaOku << " ---> işlendi. \n";
gecici = input.get();
while (!input.eof())
{
gecici = input.get();
}
input.close();
}

void Sezar::sifrele(char *dosyaOku, char *dosyaYaz, int key)
{
ifstream input;
ofstream output;
char gecici;
input.open(dosyaOku);
output.open(dosyaYaz);
gecici = input.get();
while (!input.eof())
{
if (gecici >= 'A' && gecici <= 'Z')
{
gecici = (gecici + key);
if (gecici > 'Z')
{
gecici = ((gecici % 'Z') + 'A') - 1;
}
}

if (gecici >= 'a' && gecici <= 'z')
{
gecici = (gecici + key);
if (gecici > 'z')
{
gecici = ((gecici % 'z') + 'a') - 1;
}
}
output.put(gecici);
gecici = input.get();
}
input.close();
output.close();
yaziOku(dosyaOku);
yaziOku(dosyaYaz);
}

void Sezar::sifreCoz(char *dosyaOku, char *dosyaYaz, int key)
{
ifstream input;
ofstream output;
char gecici;
input.open(dosyaOku);
output.open(dosyaYaz);
gecici = input.get();
while (!input.eof())
{
if (gecici >= 'A' && gecici <= 'Z')
{
gecici = (gecici - key);
if (gecici > 'Z')
{
gecici = ((gecici % 'Z') + 'A') - 1;
}
}

if (gecici >= 'a' && gecici <= 'z')
{
gecici = (gecici - key);
if (gecici < 'a')
{
gecici = gecici + 26;
}
}

output.put(gecici);
gecici = input.get();
}
input.close();
output.close();
yaziOku(dosyaOku);
yaziOku(dosyaYaz);
}

int main()
{

Sezar program;
int secim, key = 3;
char dosyaOku[30], dosyaYaz[30];
system("clear");

cout << "\n Enter the file/Encrypted file to be encrypted: ";
cin >> dosyaOku;

cout << "\n Enter the name of the output file: ";
cin >> dosyaYaz;

cout << "\n\n 1. Encrypt File\n 2. Decrypt File \n\n Choose one(1 or 2): ";
cin >> secim;
if (secim == 1)
program.sifrele(dosyaOku, dosyaYaz, key);

else if (secim == 2)
program.sifreCoz(dosyaOku, dosyaYaz, key);

else
cout << "\n\n Wrong choice.";

getchar();
}
我试图添加此代码,但我无法弄清楚
    std::string path = "../Texts";

for(const auto &entry:filesystem::directory_iterator(path))
{
std::cout << entry.path() << std::endl;
}
编译错误:
       error: error: ‘filesystem’ has not been declared
137 | for(const auto &entry:filesystem::directory_iterator(path))
| ^~~~~~~~~~

Build finished with error(s).
The terminal process failed to launch (exit code: -1).

最佳答案

实现目录遍历的一种相当老派的方法如下所示:

#include <dirent.h>

if (struct *d = dir("../Texts"))
{
while (struct dirent *e = readdir(d))
{
if (*e->d_name == '.')
continue;
//Do your stuff.
}
closedir(d);
}
请引用 https://linux.die.net/man/3/readdir有关如何使用它的详细信息。您可能还想查看 stat , 来辨别您正在查看的文件是目录、符号链接(symbolic link)还是文件。
请注意, d_name仅包含相对于您使用 opendir 打开的目录的文件名,意思是,为了获得实际的文件路径,您需要将它与路径连接起来。

关于C++读取多个文本文件(Ubuntu),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72570221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com