Dynamic Arrays w/Text Files

I've just learned about Dynamic Arrays and now I'm trying to read from a txt file into a dynamically allocated array. I was thinking I could include the number of records as metadata in the first line of the file in order to accomplish this, but I'm not sure how to do that exactly.



Example of txt file contents
Iron Man 487
Captain America 568
The Hulk 17
Black Widow 542
Spider Man 309
Scarlet Witch 184
Winter Soldier 237
Just Thor 491
The Falcon 319

I understand how to put numbers into a dynamic array from a file, but can't find anything on reading names AND numbers from a file into a dynamic array and using variables for both afterward for storing.

I've included some rough beginning code below just to show what I know so far without finishing it.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <vector>


using namespace std;

int main() {

       vector<int> numbers;
       ifstream in ("names.txt");

       int number; //variable to hold numbers 









}


names AND numbers from a file into a dynamic array


Dynamic array of what? int? char? string? Are you trying to store the entire contents of the text file in a single array of char?
closed account (E0p9LyTq)
Based on your file content you could create two std::vectors, one that contains std::string to hold the names with another that holds int.

Read each line from your file into a std::string.
http://www.cplusplus.com/reference/string/string/getline/

Construct a stringsream from your string input, extracting into a std::string and int.
http://www.cplusplus.com/reference/sstream/basic_istringstream/basic_istringstream/
http://www.cplusplus.com/forum/beginner/13117/#msg63116

When you have your separated name and number push the std::string back into the std::string vector and the int into the int vector.
http://www.cplusplus.com/reference/vector/vector/push_back/

One problem is you lose "linkage" between the name and number. They are no longer associated together as they were in the file.

There is a C++ "container", std::pair, that could keep the name and number together.
http://www.cplusplus.com/reference/utility/pair/

You'd construct a single vector that holds a pair, the pair holding a std::string and int:
std::vector<std::pair<std::string, int>> test;

You want to print out the 3rd element (test[2]) in the vector?
std::cout << test[2].first << ' ' << test[2].second << '\n';
Last edited on
closed account (E0p9LyTq)
If you are unsure how to create a std::vector that contains std::string:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <vector>
#include <iostream>
#include <string>


int main()
{
   std::vector<std::string> strings;

   strings.push_back(std::string("Hello"));
   strings.push_back(std::string("Good-bye"));
   strings.push_back(std::string("Up is Down"));
   strings.push_back(std::string("Good-bye Cruel World"));

   for (auto itr = strings.cbegin(); itr != strings.cend(); itr++)
   {
      std::cout << *itr << '\n';
   }
   std::cout << '\n';
}
Dynamic array of what? int? char? string? Are you trying to store the entire contents of the text file in a single array of char?


@ Repeater

I want to store/read the entire text file into a dynamic array

The output would ideally and eventually be listing all of the names with the numbers (lets say they're votes) after and then I'll have it make a percentage for each persons votes and output that with a "winner"
@FurryGuy

So I could create two different Vectors...both reading from the txt file, but one for int and one for char?

There isn't a more cohesive way to have them go together though?
closed account (E0p9LyTq)
Create two different vectors, one to hold std::strings for the names, another to hold int for the numbers.
1
2
std::vector<std::string> names;
std::vector<int> numbers;

std::strings are MUCH easier to deal with than char arrays.

Open your file and loop until you've read all the names and numbers. Or you get an error.

Each loop read a name into a temporary string and push it back into your std::string vector.
1
2
3
std::string temp;
in >> temp_name;
names.push_back(temp_name);

Each loop read a number into a temporary int and push it back into your int vector.
1
2
3
int temp_num;
in >> temp_int;
numbers.push_back(temp_num);

Repeat until you've read all your data.
Okay @FurryGuy, thank you for your guidance! So I've applied that concept and so far I have this and showing no errors.

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
 #include <iostream>
#include <fstream>
#include <vector>
#include <string>



using namespace std;

int main() {

	vector<int> numbers;   //dynamic array to hold votes

	ifstream in("candidates.txt"); //input file stream 

	int number; //variable to hold votes while being read



	while (in >> number) {

		numbers.push_back(number);
	}

	in.close();
    
{
	vector<string> names; //dynamic array to hold names

	ifstream in("candidates.txt"); //input file stream

	string name; //variable to hold names while being read

	while (in >> name) {

		names.push_back(name);
	}
}
	in.close();

	










}



So next my practice would be to "cout" the information with the percentage of "votes" each hero got either after or beside somehow?, not sure how that would work yet and then I want to display the "winner"
Last edited on
vector<int> numbers; //dynamic array to hold votes

A vector isn't an array. Do you actually want to use an array, or when you say "dynamic array" do you actually mean "some kind of sensible, appropriate storage class"?
Another option would be to a struct with name and number. Since the information belong together I don't see a point to separate them.

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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <cstdio>

using namespace std;

struct Data
{
  string name;
  int number;
};


int main()
{
  vector<Data> items;
  ifstream in("candidates.txt"); //input file stream
  if (!in)
  {
    perror(nullptr);
    return errno;
  }
  string s1, s2;
  int number;
  // assumes that names are separated by whitespace
  while (in >> s1 >> s2 >> number)
  {
    Data data;
    data.name = s1 + " " + s2;
    data.number = number;
    items.push_back(data);
  }

  // print the data to see if we read them properly

  for (const Data& data : items)
  {
    cout << data.name << " " << data.number << "\n";
  }
}
@Repeater
In this context I was meaning "some kind of sensible, appropriate storage class".
I would love to read the information from the txt file into a dynamically allocated array, but I'm having too much trouble figuring out, so I went with vector.
Topic archived. No new replies allowed.