Thanks for the help Your code works great for a single line, but when I attempted to use it to read multiple lines using getline it does not get a second line. Am I missing something, as I have never used a 2d vector. I really appreciate the help, the sad part is I figured the algorithm for decryption out easily... i just cant seem to get the correct data into an 2d array/vector
ALl I need to do is read EACH line of the text file into an array/vector row and then every token into a column. so that if the text file was:
I did change from using the hard coded string to using line, I really appreciate what you showed me thus far... looks like it would take a lot less code to accomplish this now, since I will not have to loop through the file one time to get the size to create arrays. Thanks Again
1 2 3 4
|
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 .
|
my array/vector would need to be
ROW 1 ----> I ' m s o r 0 y 4 D a v e ,
ROW 2----->5 12 12 12 3 7 f 11 2 i d 5 8 1 c 5 n
ROW 3----->EMPTY
ROW 4----->10 t 5 7 17 2 3 h 7 2 .
once I get it into a 2d vector, I need to know the height & MAX width so I can use a loop to access it via encryptedarray[col][row] and then perform processing on each string of the array to decrypt it to decryptedarray like I was doing earlier when I couldn't capture the null space.
Commented my code out and was editing line by line until i realized I was not getting the second line
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <stdio.h>
#include <sstream>
#include <cctype>
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
int main()
{
std::string file_path;
std::string line;
int listCount=0;
int row=0;
int col=0;
int maxcol=0;
const std::string space( 1, ' ' ) ;
const std::string three_spaces = space + space + space ;
const char nullchar = 0 ;
std::vector<std::string> encryptedarray1;
std::vector<std::string> decryptedarray1;
//Get file path & name & store in file_path
//cout << "What is the location of the file to decrpyt?"<< std::endl;
//cin >> file_path;
file_path = "c:/test.txt";
//Open Stream from file
std::ifstream fin;
fin.open(file_path);
if (fin.is_open())
{
while ( getline (fin ,line) )
{
std::string linestring = line;
auto pos = line.find(three_spaces) ;
if( pos != std::string::npos ) // if found
{
// the part before three consecutive spaces
std::string part_one = line.substr( 0, pos ) ;
// the part after three consecutive spaces
std::string part_two = line.substr( pos + three_spaces.size() ) ;
line = part_one + space + nullchar + space + part_two ;
// parse this string as in the earlier code
//std::vector<std::string> array ;
col = 0;
std::istringstream stm(line) ;
std::string temp ;
while( stm >> temp )
{
// except that if it is a nullchar, replace it with a space
if( temp.front() == nullchar ) encryptedarray1.push_back(space) ;
else encryptedarray1.push_back(temp) ;
if(!is_number(temp))
{
listCount+=1;
}
col+=1;
if (maxcol<col)
{
maxcol=col;
}
}
// dump the contents of the array to check it out
int n = 0 ;
for( const std::string& token : encryptedarray1 )
std::cout << ++n << ". \"" << token << "\"\n" ;
std::string linestring = line;
col = 0;
std::cout << linestring << "line #"<< row << std::endl << std::endl;
/*
std::stringstream os(linestring);
while (os >> temp)
{
//std::cout <<temp <<std::endl;
if(!is_number(temp))
{
listCount+=1;
}
col+=1;
if (maxcol<col)
{
maxcol=col;
}
}
row+=1;
*/
}
}
}
else
{
std::cout << "Unable to open file";
}
/* COMMENTED OUT FROM HERE TO END FOR TESTING
//Now that i have looped through the file once to get the sizes, i clear the flags and reopen the file
fin.clear();
fin.close();
fin.open(file_path);
if (fin.is_open())
{
//create the array based on sizes obtained above
std::string **encryptedarray = new std::string*[maxcol];
for(int i = 0; i < maxcol; ++i)
{
encryptedarray[i] = new std::string[row];
}
std::string **decryptedarray = new std::string*[maxcol];
for(int i = 0; i < maxcol; ++i)
{
decryptedarray[i] = new std::string[row];
}
std::string listArray[300];
row = 0;
while ( getline (fin ,line) )
{
std::string linestring = line;
std::stringstream os(linestring);
std::string temp;
col=0;
//os >> std::noskipws;
while (os >> temp)
{
std::cout << "Next character read from the stream is " << temp <<std::endl;
//NONE OF TRHIS WORKS UNTIL I GET THE SPACE TO BE READ FROM THE FILE AS A TOKEN/value
encryptedarray[col][row] = temp;
if(is_number(temp))
{
//IF temp is a number assign it to value
int value = std::stoi(temp);
decryptedarray[col][row]= listArray[value];
std::string swap = listArray[value];
for(int i=value;i>0;--i)
{
listArray[i]=listArray[i-1];
//std::cout << "list array values " << listArray[i] <<std::endl;
}
listArray[0]=swap;
//std::cout << "number found in temp " << temp <<std::endl;
//std::cout << "number found in value " << value <<std::endl;
}
else
{
decryptedarray[col][row] = temp;
std::cout << "The current list is now ";
for(int i=listCount;i>0;--i)
{
listArray[i]=listArray[i-1];
std::cout << listArray[i];
}
listArray[0]=temp;
std::cout << std::endl<<std::endl;
system("PAUSE");
std::cout << std::endl<<std::endl;
}
col+=1;
}
row+=1;
}
for(int r=0;r<row;++r)
{
for(int i=0;i<maxcol;++i)
{
std::cout << encryptedarray[i][r];
}
}
std::cout <<std::endl;
for(int r=0;r<row;++r)
{
for(int i=0;i<maxcol;++i)
{
std::cout << decryptedarray[i][r];
}
}
}
else
{
std::cout << "Unable to open file";
}
system("PAUSE");
*/
system("PAUSE");
}
|