how to read this text file?

First of all here is the file:

3
4
cat1 4 25
cat2 3 59
cat3 4 15
cat4 6 20
2
cat5 6 45
cat6 3 55
5
cat7 3 58
cat8 4 2
cat9 4 5
cat10 4 4
cat11 4 6


Ok, so the problem i have ran into is that the first number (3), is the number of groups, and it can change. And because it can change i got confused how can i declare ints by not knowing how many there will be?
Last edited on
but there aren't 3 groups there are zero groups, because the next line down is saying to expect 4 groups.

If I'm getting what you're trying to explain I would expect 3 'catX ...' lines under your '3'.
Last edited on
I assume you mean that on each line there are three elements?

In that case, you'll either need to dynamically allocate your array, or just use a std::vector or something; considering that you have a number, assuming this is homework the person who gave it to you probably wants you to do the manual way.

A quick example so you know what you're looking at, and what you might expect you'd need to do to read the file:
1
2
3
4
5
6
7
8
9
std::string** groups = new (std::string*)[numgroups];
// assuming numgroups == 3, dynamically allocate each element:
groups[0] = new std::string[numgroups_group0];
groups[1] = new std::string[numgroups_group1];
groups[2] = new std::string[numgroups_group2];
// make sure to clean up when you're done
for (int i = 0; i < numgroups; ++i)
    delete groups[i];
delete groups;
Last edited on
Let me guess the format:
NG
[ NR [ word int int ]*NR ]*NG

The first value is number-of-groups and is followed by that many "groups".
A "group" has first a number to tell how many entries/lines/rows/triplets will follow and then that many rows.

Something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Entry {
  // something clever
};

int main() {
  using Group = std::vector<Entry>;
  std::vector<Group> data;
  size_t NG;
  std::cin >> NG;
  data.reserve(NG);
  for ( size_t group = 0; group < NG; ++group ) {
    size_t NR;
    std::cin >> NR;
    Group foo;
    foo.reserve(NR);

    // read Entries of one group into foo

    data.push_back( foo );
  }

  // use data
  return 0;
}
but there aren't 3 groups there are zero groups

The first group is
1
2
3
4
cat1 4 25
cat2 3 59
cat3 4 15
cat4 6 20 

which contains 4 elements
The second group is
1
2
cat5 6 45
cat6 3 55 

which contains 2 elements and so forth


3   <--  so what the hell is this '3' about?
 4 
 cat1 4 25 
 cat2 3 59 
 cat3 4 15 
 cat4 6 20 
 2 
 cat5 6 45 
 cat6 3 55 
 5 
 cat7 3 58 
 cat8 4 2 
It says how many groups there are. So in this case there will be 3 groups of cats which can contain 2-8 cats. if three would be a const there would be no problem, but that number can change, and there can be more groups
Last edited on
oh okay. looks like keskiverto gave you a good start then.
looks like keskiverto gave you a good start then.

But i dont know vectors yet :/. in this i should use the most simple stuff, structures, loops, char/string
There are three obvious choices:

1. Dynamically allocate memory. Use std::vector to do it. Seems to be ruled out.

2. Dynamically allocate memory. Use language primitives to do it. Educational, but not really simpler.

3. Statically allocate "enough" memory. Crude, but "simple".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main () {
  constexpr int N = 42; // We hope to never have more than this.
  double foo_arr[ N ]; // static array
  int foo_len;
  std::cin >> foo_len;
  if ( N < foo_len ) {
    // user want's more than we can store. Eject!
    std::cout << "Please, recompile the program with a bigger N\n";
    return 1;
  } else {
    for ( int i = 0; i < foo_len; ++i ) {
      foo_arr[i] = 3*i;
    }

    for ( int i = 0; i < foo_len; ++i ) {
      std::cout << foo_arr[i] << '\n';
    }
  }
  return 0;
}
Topic archived. No new replies allowed.