I have tried to read from a text file and using a loop put the two lines in the text file into a string array. But I want to make the array( in my code: string abc[5]) a variable size array without using pointers.I am fairly new to c++, Can someone please help me with that. Thanks in advance.
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
#include<sstream>
#include<vector>
using namespace std;
int main() {
string line;
string iss_two;
int i = 0;
ifstream myfile("example.txt");
if (myfile.is_open())
{
string token;
stringstream iss;
int x = 0;
string abc[5];
while (getline(myfile, line)) // first read entire line into a
//string
{
abc[i] = line;
cout << abc[i] << "\n";
i++;
// cout << token;
}
//iss.clear();
cout << "\n" << abc[0];
cout << "\n" << abc[1];
myfile.close();
}
else cout << "Unable to open file";
system("pause");
return 0;
}
std::vector.std::vectorshould be the first thing that pops up in your mind when you want any type of array or list like object in C++. You probably never want to use an[]-array and only very rarely a pointer of any kind in C++.#include <vector>in your code, so why did you not usestd::vector?