2d Array vs vector from a space delimited text file

OK, so this IS a homework question. I have Never used C++ but for my computer security class the prof has chosen C++. First I will past the assignment so you will know why I chose the route I have chosen.

Assignment:
1. One of your fellow students has created an encryption scheme that she feels would be useful for protecting cell phone text messages. The procedure encrypts a plain-text file by reading in sequence each character in the file. The letter is copied to the encrypted file if it has not been encountered previously in the plain-text file. The letter is also copied to the head of a list. If this is not the first occurrence of the letter, the list position of the letter (as opposed to the letter itself) is put in the encrypted file and the letter is moved to the head of the list. Each character within each line of the encrypted file is delimited by a single space. The space character itself is denoted by three consecutive spaces (one space for each of the delimiters and one space for the space character itself). Line breaks are preserved in their plain-text form. The list starts at position 0.

2. You are to write a program in C/C++ that decrypts an encrypted file, that executes in the class virtual machine. This is the only machine that your code will be tested on. You may assume that the original unencrypted message contains no digits.

3. Input: The input will be a text file of indeterminate length.

4. Output: You are to print the encrypted message followed by the decrypted message.

Sample:
Encrypted message:
I ' m s o r 0 y 4 D a v e , 5 12 12 12 3 7 f 11 2 i d 5 8 1 c 5 n 10 t 5 7 17 2 3 h 7 2 .
Decrypted message:
I'm sorry Dave, I'm afraid I can't do that.
END Assignment

Now knowing that I need to keep the identical layout of the encrypted message I decided first to try to use an array, but since the line size could vary that ruled using an array out (or am I wrong)? So anyway I decided to use vectors as this seemed like the solution most recommended for undetermined array sizes.

So far I loop through the text file line by line getting each value and place it inside encrpytedvalue using some code from "how to parse a text file". The problem I see with this code is that it requires a constant MAX_CHARS_PER_LINE and a MAX_VALUES_PER_LINE (I just caught that while reviewing the code.)

My next problem .. even if I overcame the MAX_..VALUES issue I decribed about .. is how to get values assigned to the vector location (encryptedmatrix{0][2])

There must be a much more simpler way to read this text file into an array/vector that I am missing .... I must just be overthinking this and causing myself problems.

Here is the code thus far, and it is way to much code for such a simple task as reading a space delimited text file into an array.

Line 117 is where I have tried assigning string values into the vector ( I removed it cause I am pulling my hair out)

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <fstream>
using std::ifstream;

#include <vector>;
#include <string>
using std::string;

#include <cstring>;

#include <stdio.h>;





const int MAX_CHARS_PER_LINE = 512;
const int MAX_VALUES_PER_LINE = 20;
const char* const DELIMITER = " ";


int main()
{
	//REMOVE AFTER CODING, ONLY HERE TO TEST
	int ArrayWidth = 0;
	int ArrayHeight = 0;
	string init_value = "#$";
	//WILL NEED TO REMOVE FROM CODE BELOW AS WELL
	//END REMOVE AFTER CODING

	std::vector< std::vector<string> > encryptedmatrix;
	std::vector< std::vector<string> > unencryptedmatrix;
	string file_path;						
	int i = 0;


	//Get file path & name & store in file_path
	cout << "What is the location of the file to decrpyt?"<< std::endl;
	cin >> file_path;
	
	//Open Stream from file
	ifstream fin;
	fin.open(file_path);
	if (!fin.good()) 
		return 1;
  		while (!fin.eof())
		{
			char buf[MAX_CHARS_PER_LINE];
			fin.getline(buf, MAX_CHARS_PER_LINE);
    
			int n = 0; 
    		// array for mem addresses of the encrypted values
			char* encrpytedvalue[MAX_VALUES_PER_LINE] = {};
    
			// parse the line
			encrpytedvalue[0] = strtok(buf, DELIMITER);				// first encrypted value
			if (encrpytedvalue[0])									// if line is blank
			{
				for (n = 1; n < MAX_VALUES_PER_LINE; n++)
				{
					encrpytedvalue[n] = strtok(0, DELIMITER);		// next encrypted value
					if (!encrpytedvalue[n]) break; // no more tokens
					if (ArrayWidth < n)
					{
						ArrayWidth = n;
					}
				}
				ArrayHeight+=1;
			}
			for (i = 0; i < n; i++) // n = #of tokens
		
			cout << "Encrypted Values[" << i << "] = " << encrpytedvalue[i] << endl;
			cout << endl;
		}
		//Line to dispay the size of the needed vector/array

		cout << ArrayWidth << " Characters Wide x " << ArrayHeight << " Lines Tall" << endl;
		system("PAUSE");
		
		// LOOPING THROUGH A SECOND TIME NOW  THAT SIZE REQUIREMENTS HAVE BEEN DETERMINED
		fin.clear();
		fin.close();
		fin.open(file_path);
		//RESIZE ARRAY
		encryptedmatrix.resize( ArrayWidth, std::vector<string> (ArrayHeight, init_value));
		unencryptedmatrix.resize( ArrayWidth, std::vector<string> (ArrayHeight, init_value));
		//RESET Height Counter
		ArrayHeight=0;
		
		while (!fin.eof())
		{
			char buf[MAX_CHARS_PER_LINE];
			fin.getline(buf, MAX_CHARS_PER_LINE);
    
			int n = 0; 
    		// array for mem addresses of the encrypted values
			char* encrpytedvalue[MAX_VALUES_PER_LINE] = {};
    
			// parse the line
			encrpytedvalue[0] = strtok(buf, DELIMITER);				// first encrypted value
			if (encrpytedvalue[0])									// if line is blank
			{
				for (n = 1; n < MAX_VALUES_PER_LINE; n++)
				{
					encrpytedvalue[n] = strtok(0, DELIMITER);		// next encrypted value
					if (!encrpytedvalue[n]) break; // no more tokens
				}
				
			}
			for (i = 0; i < n; i++) // n = #of tokens
			
			//test code to display what values should be placed in encryptedmatrix
			///STUCK HERE .. how do i get values into the vector encryptedmatrix .. i keep getting subscript out of range
			cout << "Matrix Values[" << i << "] = " << encrpytedvalue[i] <<" needs to go into matrix ["<<i<<"]["<<ArrayHeight<<"]"<< endl;
			ArrayHeight+=1;
			cout << endl;
		}
		system("PAUSE");

		//POPULATE the LIST used for encryption based on max value in encrypted array (convert num to int)

		//once vector encryptedmatrix is loaded, copy all non numeric characters to unencrptyedmatrix

		//WRITE algorithm to decrypted remaining chars

		//WRITE decrypted chars to unencryptedmatrix

		//Write Unencrpytedmatric to the screen line by line (matching the input file) ie.. the have to have the same line length, which is why i used a vector

}
Last edited on
After continuing to read and pull my hair out I think getline may be the way to go here, but have only seemed to bang my head against the wall the past hour and a half.
I am thinking I could use getline and then break this into an array/vector using the space as the delimiter. I could count the number of values from each line and use it to size the number of columns needed. Or I am going backwards?

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
int main()


{
	string file_path;
	string line[4095];
	int i=0;
	std::vector< std::vector<string> > encryptedArray;
	//Get file path & name & store in file_path
	cout << "What is the location of the file to decrpyt?"<< std::endl;
	cin >> file_path;
	//Open Stream from file
	ifstream fin;
	fin.open(file_path);
	if (fin.is_open())
	{
		while ( getline (fin ,line[i]) )
		{
			//NOW TO BREAK THIS UP AND PUT IN ARRAY/VECTOR
                                             cout << line[i] << "line #"<< i<< endl;
			i+=1;
		}
		fin.close();
		system("PAUSE");
	}

	else cout << "Unable to open file"; 

	return 0;
}
Topic archived. No new replies allowed.