Question about reading text files

Pages: 12
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int extract_from_file(int length, char *buffer, ifstream &file, char *text)
{
    file.getline(buffer, length);
    
    char ch;
    int i{0};
    int sz{0};
    
    while(buffer[i] != '\0')
    {
        ch = buffer[i];
        if(isalpha(ch))
        {
            text[sz] = buffer[i];
            sz++;
        }
        i++;
    }
    text[sz] = '\0';
    return sz;
}

int main ()
{
    const int KEY_LENGTH{7};
    const int MAX_LENGTH{60};
    
    char buffer[MAX_LENGTH];
    char key[KEY_LENGTH];
    char plain_text[MAX_LENGTH];
    
    ifstream in_file ("encrypted_cats.txt");
    
    if (!in_file.is_open())
    {
        cout << "Unable to open file\n";
        return -1;
    }
    
    int   sz_key = extract_from_file(MAX_LENGTH, buffer, in_file, key);
    int sz_plain = extract_from_file(MAX_LENGTH, buffer, in_file, plain_text);
    
    cout << key << ' ' << sz_key << '\n';
    cout << plain_text << ' ' << sz_plain << '\n';
    
    for(int i = 0; i < sz_key; i++)
    {
        cout << key[i] << ' ';
    }
    cout << '\n';
    
    int count{0};
    for(int i = 0; i < sz_plain; i++)
    {
        cout << plain_text[i] << ' ';
        count++;
        if(count % sz_key == 0)
            cout << '\n';
    }
    cout << '\n';
    
    in_file.close();
    return 0;
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12