New kid on the block

Pages: 12
My question is, how does the while(getline(file, str)) work if the getline function isn't a bool function?


std::getline returns a reference to the istream that was passed in. When an istream is used in a boolean condition, the condition is false if the stream is bad (i.e. at least one error flag is set. This includes the eofbit) and true otherwise. This is accomplished via an overloaded void* cast operator provided by istream.
Hey again guys, Here's the code i'm using and it uses a pointer to the text file that i can then index through the pointer (somehow, dont ask me).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

char* text;
int length = 0;

void login();
void read();
void encrypt();
void decrypt();

int main(){
    int a = 0;
    login();
    cout << "\nBefore using this program please ensure your data is placed in the text ";
    cout << "\ndocument named \"target.txt\" and placed in same location as the application!";
    do{
        cout << "\n\n0: Quit!\n1: Encrypt?\n2: Decrypt?\n\nPlease Select An Option: ";
        cin >> a;
        switch (a){
            case 0 : return 0; break;
            case 1 : system("CLS"); encrypt(); break;
            case 2 : system("CLS"); decrypt(); break;
            default : cout << "Invalid Selection!";
        }
    }while(a != 0);
    return 0;
}

void login(){
    string user = "SatsumaBenji";
    string pass = "Password";
    string userTry;
    string passTry;
    cout << "\n\n\tYou Must Login Before Using This Application!\n";
    do{
        cout << "\nUsername: ";
        cin >> userTry;
        cout << "Password: ";
        cin >> passTry;
        system("CLS");
        cout << "\n\n\tIncorrect Username or Password!\n";
    }while((userTry != user) || (passTry != pass));
    system("CLS");
    return;
}

void read(){
    
    ifstream ifile;
    ifile.open("target.txt", ios::binary);
    if(ifile.is_open()){
        ifile.seekg(0, ios::end); //Get length of file
        length = ifile.tellg();
        ifile.seekg(0, ios::beg); //Reset Position of pointer to 0
        text = new char[length]; //Set length of char* to file length
        ifile.read(text, length); //Read File upto Length limit
        ifile.close();
    }
    else
    cout << "\nError Opening File To Read!\n";
    return;
}

void encrypt(){
    int temp;
    char out;

    read();
    ofstream ofile;
    ofile.open("target.txt", ios::binary | ios::trunc);
    if(ofile.is_open()){
        for(int i=0; i<length; ++i){
            temp = text[i];
            temp = (static_cast<int>(temp));
            temp = temp+((i+1)*3);
            out = (static_cast<char>(temp));
            ofile << out;
        }//For
        ofile.close();
        cout << "\nFile Encryption Complete!" << endl;
    }//If file open
    else
    cout << "\nError Opening File To Write!\n";
    delete[] text;
    return;
}//encrypt()


void decrypt(){
    int temp;
    char out;

    read();
    ofstream ofile;
    ofile.open ("target.txt", ios::binary|ios::trunc);
    if(ofile.is_open()){
        for(int i=0; i<length; ++i){
            temp = text[i];
            temp = (static_cast<int>(temp));
            temp = temp-((i+1)*3);
            out = (static_cast<char>(temp));
            ofile << out;
        }//For
        ofile.close();
        cout << "\nFile Encryption Complete!" << endl;
    }//If file open
    else
    cout << "\nError Opening File To Write!\n";
    delete[] text;
    return;
}//decrypt() 


This code has been edited and now the program works Exactly how it should!
(EDIT: ios::out changed to ios::binary, and character pointer is deallocated at end of en/decryption to prevent wasted computer memory
Last edited on
You could have the user input the file path of the text file they want to encrypt or decrypt and put the result anywhere they want by inputting another file path. It wouldn't be that hard to integrate and it would allow the user to choose any file rather than having to use a file you chose for them.
I have tried this but the system function to open a directory wont accept a string i give it, it'll only accept constant literals that are entered as

system("cd C:\User\SatsumaBenji\Desktop")
But it WONT accept this..
1
2
3
4
5
6
7
8
string fnctn = "cd ";
string in;
string dir;

cout << please enter a directory: ";
cin >> in;
dir = (fnctn + in);
system(dir); 


I don't know why it wont accept strings but i assume it's todo with the way the function is set out, unfortunately this is the same case with the file.open("filename") so for now i have no way for the user to decide save settings.



P.S. I have found out the problem with the above code and i have edited it to how it should be (the program now works absolutely PERFECTLY)
AHA!! I've accidently stumbled across how to send a string to the file.open() function and have now encorporated this into my program (after a little confusion between system directory and file directory) the user can now specify the file's name and directory if they wish and if they don't then default directory is where the program is stored and the default file name is still "target.txt".

P.S. the way i did this was file.open(strname.c_str());
system is a C library function, so there's no reason it would take a std::string.

The fstream family, on the other hand, will take a std::string in C++11 compliant compilers without resorting to calling the c_str method.
Well like i said there was a little confusion between system directory and file directory, I didn't need any system functions at all.
I simply used a string to save custom directory if wanted and another to save custom file name if wanted, then a third string that adds them both (or default if unaltered) and uses that third string in the file.open(str3.c_str());

Anyways it's all good and now there's only a few different things i want to add/change to the program before i say to myself it's at its peak (i want to change to a bit encryption method (moving bits to alter char), and i want the program to recognize if it's already fully decryption or not)
Topic archived. No new replies allowed.
Pages: 12