C++ Dynamic Array & Ifstream

Oct 26, 2012 at 11:44pm
I have a question about the problem I'm working on right now. What I'm suppose to do is read text from a txt file into a dynamic array. So what I need to do is read how many chars are in the txt file and set that as my size for my dynamic array. The problem is I'm not sure how to read and count how many chars are in the txt file. Any help would be appreciated. Thanks in advance.
Oct 28, 2012 at 3:37am
Oct 28, 2012 at 6:42pm
I know how to read from a file, I'm just not sure how to go about reading it into a dynamic array. Also since I have to manipulate the letters would it be easier to put in a char array or string array?
Oct 28, 2012 at 7:01pm
if you need array of int

int *arr

//read the number of elements
arr = new int[Number_of_elements];

for( i= 0; i < number_of_elements; ++i )
cin >> arr[i];

//replace cin with your file stream

//do stuff
delete arr;
arr = NULL; //if using C++11 capable compiler use nullptr instead
Topic archived. No new replies allowed.