为什么我的代码不写入 C++ 中的文件?

发布于 2025-01-21 03:00:35 字数 2073 浏览 2 评论 0原文

我编写了一个获取输入数据的代码,将其写入字符串,然后将其发送到加密的临时文件,然后临时将加密的文件发送到主文件。我设法以某种方式使它起作用(只有寄存器),但我没有对代码做任何事情,现在数据没有写在文件上。任何类型的帮助都将受到欢迎。谢谢!

#include<iostream>
#include<istream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>


using namespace std;

void login();
void reg();
void encrypt();

int main() {

    int rep;
    cout << "\t\t\t Welcome to Protek 0.1.\n" << "\t\t\t Do you already have a account?\n\n";
    cout << "\t\t\t\t 1 = YES    2 = NO\n";
    cin >> rep;

    
    switch (rep) {
        case 1:
        login();
        break;

        case 2:
        reg();
        break;

        default:
        break;
    }

 
}

void login() {
    int count;
    string LUserN, LUserP, LN, LP;

    system("cls");


    cout << "Please enter your name.";
    cin >> LUserN;

    cout << "Please enter your pass.";
    cin >> LUserP;

    ifstream input("data/pn.txt");
    while (input>>LN>>LP) {
        if (LN==LUserN && LP==LUserP) {
            count = 1;
     }
    }
    input.close();

    if (count == 1) {

        cout << "\t\t\t SUCCESS! \n";
        cin.get();
        cin.get();
        main();
    }

    else {
        cout << "\t\t\t ERROR";
        main();
    }
}

void reg() {
    string RUserN, RUserP;
    system("cls");

    cout << "\t\t\t Enter your username. \n";
    cin >> RUserN;

    cout << "\t\t\t Enter pass. \n";
    cin >> RUserP;

    encrypt();

    cout<< "\t\t\t Reg Complete!\n";
    main();


}

void encrypt() {
    char ch;
    fstream fps, fpt;
    fps.open("data/pn.txt", fstream::in);
    fpt.open("data/pt.txt", fstream::out);

    while (fps >> ch) {
        ch = ch * 22;
        fpt << ch;
    }

    fps.close();
    fpt.close();
    
 fps.open("data/pn.txt", fstream::out);
    fpt.open("data/pt.txt", fstream::in);

    while (fpt >> ch) 
        fps << ch;
        fpt.close();
        fps.close();

 
}

I wrote a code that takes input data, writes it on a string then it is sent to a temp file that gets encrypted and the temp sends the encrypted file to the main file. Somehow I managed to make it work (only the register) but I didn't do anything to the code and now the data isn't written on the files. Any kind of help will be welcomed. Thanks!

#include<iostream>
#include<istream>
#include<fstream>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>


using namespace std;

void login();
void reg();
void encrypt();

int main() {

    int rep;
    cout << "\t\t\t Welcome to Protek 0.1.\n" << "\t\t\t Do you already have a account?\n\n";
    cout << "\t\t\t\t 1 = YES    2 = NO\n";
    cin >> rep;

    
    switch (rep) {
        case 1:
        login();
        break;

        case 2:
        reg();
        break;

        default:
        break;
    }

 
}

void login() {
    int count;
    string LUserN, LUserP, LN, LP;

    system("cls");


    cout << "Please enter your name.";
    cin >> LUserN;

    cout << "Please enter your pass.";
    cin >> LUserP;

    ifstream input("data/pn.txt");
    while (input>>LN>>LP) {
        if (LN==LUserN && LP==LUserP) {
            count = 1;
     }
    }
    input.close();

    if (count == 1) {

        cout << "\t\t\t SUCCESS! \n";
        cin.get();
        cin.get();
        main();
    }

    else {
        cout << "\t\t\t ERROR";
        main();
    }
}

void reg() {
    string RUserN, RUserP;
    system("cls");

    cout << "\t\t\t Enter your username. \n";
    cin >> RUserN;

    cout << "\t\t\t Enter pass. \n";
    cin >> RUserP;

    encrypt();

    cout<< "\t\t\t Reg Complete!\n";
    main();


}

void encrypt() {
    char ch;
    fstream fps, fpt;
    fps.open("data/pn.txt", fstream::in);
    fpt.open("data/pt.txt", fstream::out);

    while (fps >> ch) {
        ch = ch * 22;
        fpt << ch;
    }

    fps.close();
    fpt.close();
    
 fps.open("data/pn.txt", fstream::out);
    fpt.open("data/pt.txt", fstream::in);

    while (fpt >> ch) 
        fps << ch;
        fpt.close();
        fps.close();

 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文