The purpose of my program is to open a text file of m lines of the same length n, read the file column by column and print each column.
For example, for this text file
abcd
efgh
jklm
I would like to print
a e j
b f k
c g l
d h m
As one line length can be 200 000 000 and the column length can be more than 10 000, I can't open all the file in memory in a matrix.
Theoretically, I would like to have a program that use O(m) in space and O(m*n) in time.
At the beginning, I had to think about these solutions:
- if I see all the file for each column the complexity is O(m*n²),
- If I use seekg and an array of positions and jump from position to position, the complexity is O(mnlog(n)).
Last point, for some server problems, I need to use only the STL.
My last idea is to create an array of iterators of a file and initialized these iterators at the beginning of each line. After that, to see the next column, I only need to increase each iterator. This is my code
ifstream str2;
str2.open ("Input/test.data", ifstream::in);
int nbline = 3;
int nbcolumn = 4;
int x = 0;
istreambuf_iterator<char> istart (str2);
istreambuf_iterator<char> iend ;
istreambuf_iterator<char>* iarray;
iarray = new istreambuf_iterator<char>[nbline];
while (istart != iend){
if (x % nbcolumn == 0){
iarray[x/nbcolumn] = istart;
}
istart++;
x++;
}
for (int j = 0; j<nbcolumn;j++){
for (int i = 0; i<nbline;i++){
cout << *iarray[i] << "\t";
iarray[i]++;
}
cout << endl;
}
Sadly, it does not work, and I have this thing as output
a e f
� � �
� � �
� � �
I think the problem is that the array of iterators iarray are not independent of istart, how I can do that?
a e j
, nota e f
. – Mythosseekg
(and why would you need an array of positions instead of calculated offsets)? It's the logarithmic factor that looks wrong to me; where did that come from? – Lugworm