Question about reading text files

Pages: 12
If a text file contains only 2 lines, is it possible to read each line into an array of characters separately? Like line one is stored in an array, and line two is stored in another.

for example, if the file contents are:

CATS
I LOVE CATS & DOGS

then array1 holds "CATS" and array2 holds "ILOVECATSDOGS"

Say I wanted to print out array1
 
cout << array1 << endl;

Output is:
C
A
T
S

Same thing with array2
 
cout << array2 << endl;

Output is:
I
L
O
V
E
C
A
T
S
D
O
G
S
Last edited on
Of course.

Have you tried to do it?
First of all, thank you for answering. Second, yes I have. I am not allowed to use the string class from c++, just string functions from c. All I need is to be pointed in the right direction. For example, textfile contains:

CATS
I LOVE CATS & DOGS

I need an array that holds the characters "CATS" and another that holds "ILOVECATSDOGS".
Last edited on
yes.
char one[1000];
char two[1000];
file >> one;
file >> two;

Thank you for answering! However, when I try something like that it prints out

CATS
I

Just the first word from the second line. My goal is for the output to be 2 arrays of just characters, no spaces / non-alphabetical characters.
Last edited on
oh, if you want the whole line, use getline instead of << the << operators stop on white space (including space and end of line etc). if you use getline, you won't get ILOVECATSDOGS, you will get the spaces. you can manually remove them or you can figure out a loop using << to read it all mushed up. which do you want?
Last edited on
yes, and this treats it as one line. so like for example when i print out char one i get

cout << one << endl;


I get:
CATS

Instead of:
C
A
T
S

I'm so sorry if I was unclear previously. I appreciate your replies!
Last edited on
@idk6199
Why not just post your code preferably with copies of the input and output files with a clear statement by you what you expected but didn't get.
Thanks for the reply, and sure i'll make it more clear and post my code. My assignment is a weird version of a caesar encryption.
1) Read text file that contains 2 lines, each line into a different array.
- the first line is a 'key' which can be anywhere from 1-6 characters, the second line is the message to be encrypted, and the message can be anywhere from 1-60 characters.
2) create a "block" that's width is the size of the key.
3) arrange message into that block and based on each column header, encrypt the letter.

textfile contents:
CATS
I LOVE CATS & DOGS

array1 should output "CATS"
array2 should output "ILOVECATSDOGS" (as you can see, I'm supposed to read the file contents as separate characters and strip out any white spaces or anything that's not between capital A-Z)

message block should look like:
C A T S
----------
I L O V
E C A T
S D O G
S

- then somehow take each letter in each column and encrypt it. For example.
'I' becomes 'K'. This is because using an ASCII chart 'C' - 'A' = 2 so shift all the letters under C by 2.
-Anything in the second column won't shift 'A' - 'A' = 0. Third columns letters will shift by 19 letters 'T' - 'A' = 19, and 4th column will shift by 18 'S' - 'A' = 18.
-After encryption, print out the encrypted message to the screen.

I am struggling to even start. I have only been learning c++ for a week so pardon me for my idiocy, I don't know if this is too advanced of an assignment , and I'm a slow learner. I haven't learned about vectors, and was told not to use the string class but use cstring instead. So far I ask the user to input their filename (so anyone can test any file with the program). Now I'm trying to read in the contents of the textfile.
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
#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

void openFile();
void readText(); 
ifstream encryptionFile; 

int main() {
  openFile(); 
  readText();

  encryptionFile.close();

  return 0;
}
void openFile() { 
  char fileName[25];

  cout << "Enter name of file you would like to encrypt " << endl;
  cin >> fileName; 
  
  encryptionFile.open(fileName); 
  
  if (!encryptionFile.is_open()) {
    cout << "File failed to open" << endl; 
  } 
  else{
    cout << "File successfully opened!" << endl;
  } 
}

void readText(){ /*this function does not do what I want it to, 
                          which is read char by char. It has 
                          to be open ended because line one 
                          can be 1-6 characters, and line 2 can
                          be 1-60 characters*/

  int i;
  char lineOne[i];
  char lineTwo[i];

  for (i = 0; i <= 5; i++){
    encryptionFile >> lineOne[i];
    cout << lineOne[i] << endl;
  }
  for (i = 0; i <= 59; i++){
    encryptionFile >> lineTwo[i];
    cout << lineTwo[i] << endl;
  }
}


I appreciate any reply, hint/help.
Last edited on
It's a solid start, you're far from the slowest learner here. :)

So, first, this needs to be changed:
1
2
3
  int i;
  char lineOne[i];
  char lineTwo[i];

i does not have a proper value yet, and isn't a constant value, so you can't make the size of the arrays that you're declaring be i.

The key can be anywhere from 1 to 6 characters, so you want your first array to be the largest length needed. This would be 6 characters long, however depends on how you're filling this, it's best to leave a free space as the last character to be a null terminator. Because you aren't allowed to use strings for some inane reason.
char lineOne[7];

Your second line can be anywhere from 1 to 60 characters, so you want it to also be able to hold the maximum amount of characters, plus a terminating character like before.
char lineTwo[61];

Also, I encourage you to declare the iteration variables of a loop (e.g. i) in the smallest scope possible, so you should instead do:
1
2
3
4
for (int i = 0; i < 7; i++)
{
    // ...
}


However, you most likely don't want a loop like this. It says the key can be UP TO 6 characters. So if you go to 6 characters, you might go past the length of the key.

I would rather design the function closer to something like this:
1
2
3
4
5
6
7
8
9
10
11
12
void readText(){
	
  char key[7];
  char message[61];

  // http://www.cplusplus.com/reference/istream/istream/getline/ 
  encryptionFile.getline(key, 7);
  encryptionFile.getline(message, 61);
  
  cout << key << '\n';
  cout << message << '\n';
}

getline reads in the entire line and puts it into the buffer, and also fills in a terminating null character.

Now, you have the lineOne and lineTwo filled. The fun part is actually transforming those letters based off the encryption key.
BTW: You can get the actual length of the key by doing strlen(key);
Last edited on
Thank you so much!! I will attempt to work off of this information because i still need to remove the white spaces and unwanted characters/non-caps lock characters. I really appreciate your help!!
Last edited on
@idk6199
I'll leave it - other things to do - but thanks for posting your code

All the best to you for giving it a go - we all understand the sweat/pressure and are keen to help genuine cases

Cheers :)
@againtry thank you!!
Hmmmmmm
@idk6199
This shows a bit more on controlling the two char arrays:
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main ()
{
    const int KEY_LENGTH{7};
    const int MAX_LENGTH{60};
    
    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;
    }
    
    in_file.getline(key, KEY_LENGTH);
    cout << key << '\n';
    
    in_file.getline(plain_text, MAX_LENGTH);
    cout << '*' << plain_text << '*' << '\n'; //* MEANS ALL ONE STRING
    
    //TEST TO SEE WHETHER CHAR ARRAYS ARE UNDER CONTROL
    char ch;
    int count{0};
    while (plain_text[count] != '\0')
    {
        ch = plain_text[count];
        
        if(ch == ' ')
            cout << '#';
        else
            cout << ch;
        
        count++;
    }
    plain_text[count] = '\0';
    cout << '\n';
    
    in_file.close();
    return 0;
}


CATS
*I LOVE CATS & DOGS*
I#LOVE#CATS#&#DOGS
Program ended with exit code: 0
And if you want to strip out the blanks:
1
2
3
4
5
6
7
8
9
while (plain_text[count] != '\0')
    {
        ch = plain_text[count];
        
        if(ch != ' ')
            cout << ch;
        
        count++;
    }
You can strip spaces and punctuation as you read the lines in.

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
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <cctype>
using namespace std;

void read_line(istream& in, char* s, int size)
{
    char ch;
    int i = 0;
    while (in.get(ch) && ch != '\n')
        if (i < size && std::isalpha(ch))
            s[i++] = std::toupper(ch);
    s[i] = '\0';
}

int main() {
    const int MaxKey{ 6 }, MaxText{ 60 };
    char key[MaxKey+1], text[MaxText+1];

    //ifstream in("input.txt");
    istringstream in("cat\n"
                     "Hello! HEY, what's UP?\n");

    read_line(in, key, MaxKey);
    read_line(in, text, MaxText);

    cout << key << '\n';
    cout << text << '\n';
}

CAT
HELLOHEYWHATSUP

Last edited on
I know, but thks anyway
You're an amateur. :)
And a f'ken terrible one at that.
Pages: 12