0

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;
}
9
  • 8
    Use std::vector. Commented Jul 26, 2017 at 13:51
  • If other data structures are an option, use a vector Commented Jul 26, 2017 at 13:51
  • Use the standard: std::vector should 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++. Commented Jul 26, 2017 at 13:52
  • You have #include <vector> in your code, so why did you not use std::vector? Commented Jul 26, 2017 at 13:56
  • I tried using vector, but I was getting some errors when I used that. Commented Jul 26, 2017 at 14:00

2 Answers 2

1

An array (as the word is used in C++ language) can not have a variable size. Its size never changes during its lifetime. The size of an array variable must be known at compile time, but the size of a dynamically allocated array can be determined at run time.

Dynamic allocation requires the use of pointer variables. You can of course hide the use of pointers by containing the functionality within a class. Such class that abstracts dynamic allocation of arrays already exist in the C++ standard library: std::vector. I recommend that you use it.

Sign up to request clarification or add additional context in comments.

Comments

0

I want to make the array( in my code: string abc[5]) a variable size array without using pointers.

If you have to use arrays, you can simulate a variable size by preallocating a really big array at compile time and then having checks to ensure you don't go over that amount (i.e. like getting a line of credit).

const int MAX_ARRAY_SIZE = 100000;
int main() {
   ...
   int linesRead = 0;
   string abc[MAX_ARRAY_SIZE];
      ...
      while (linesRead<MAX_ARRAY_SIZE && getline(myfile, line)) 
      {
          abc[linesRead] = line;
          cout << abc[linesRead] << "\n";
          linesRead++;
      }

      if(linesRead>=MAX_ARRAY_SIZE){
           cout << "Warning!  You hit the maximum array size for your "
                << "string.  There may still be text in your file\n";
      }

      ...

      cout << "\n" << abc[0];
      cout << "\n" << abc[1];
      ...
}

As pointed out by others, the std::vector library is setup for this kind of thing with C++. Also, the downfalls here include preallocating more memory than you need or not preallocating the amount you need. So, while you may use this approach to get started in debugging or if you have some additional insight into the maximum array that you would run across, you would std::vector or dynamic memory allocation otherwise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.