How to determine size of array/vector from a .txt file?

I'm supposed to make an array/vector depending on the number of rows and columns the text file has. So it must be dynamic. How do I do this?
Also, the text file contains char's so I guess I could store each line in an string array? or would a char array work better?

class Recursion
{
private:
int columnSize;
int rowSize;
char ** grid;
char symbol;
ifstream inFile;
ofstream outFile;

public:
Recursion()
{
columnSize = size of biggest column;
rowSize = size of biggest row;
grid = new char*[size of art row];
for(int i = 0; i < size of art; i++)
{
board[rowSize] = new char[size of art column]
}


}
};
Last edited on
You have to determine how large the file is.

It's typical to open the file, seek to the end, and read the offset; but don't do that. Use stat to read the file's metatdata.

e.g,
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <sys/types.h>
#include <sys/stat.h>
#include <string>

long filesize(const std::string &filename)
{
    struct stat info;
    int ret = stat(filename.c_str(), &info);
    if (ret != -1)
        return info.st_size;

    return -1;
}
Last edited on
Are you not allowed to append them to a vector?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main() {
    vector<string> v;
    string line;
    ifstream file("filename");

    while (getline(file, line))
        v.push_back(line);

    int rows = v.size();
    int cols = v[0].size(); // if all rows have same #of cols

    cout << "rows: " << rows << "  cols: " << cols << "\n";

    cout << v[0][0] << "\n"; // char in top left corner
}

This actually cleared a lot of things up for me. However, there seems to be a problem as I get 23 rows and 0 columns.

The art file I am trying to read can be found here:
http://www.chris.com/ascii/index.php?art=comics/superman

It is the 4th superman logo (the darker one).
The comment I put beside the cols variable says that it only really makes sense if all the rows are guaranteed to have the same number of columns. Apparently the first row is empty (0 columns). And the other rows look to be of different lengths (unless they are padded with spaces). So what you have is a "ragged" array. You need to ensure that you don't access beyond the edge.

I'm not entirely sure what you are trying to do so I don't know how to advise you, although you could have the vector-filling algorithm skip blank lines if that's helpful:
1
2
3
while (getline(file, line))
    if (line.size() > 0)
        v.push_back(line);

Then you should only have 22 rows and the first row should be non-zero length, but it's not the longest row.
Okay I see what you're saying. Wouldn't it be easier if I just made a box big enough to fit the logo?
I'm supposed to recursively fill the text file.
I can only do that if the user gives me the row and column they choose to fill. That's why I need to know the size of the vector/array.
Here, https://faculty.utrgv.edu/robert.schweller/CS2380/homework/hw10.pdf
Last edited on
Topic archived. No new replies allowed.