Dynamic Array using Data File

I have a data file called "data.txt" which contains an unknown amount of random numbers. I was successfully able to open that file read from it all the numbers and cout the total amount of numbers and all the numbers inside the file. I have to now convert those numbers from file into an array dynamically. I'm having trouble doing so so can someone please help me!

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

using namespace std;

int main()
{
  ifstream fin;
  int n=0;
  double temp;
  fin.open("data.txt");
  fin>>temp;
  while(fin)
  {
      n++;
      cout<<temp<<endl;
      fin>>temp;
  }
  cout<<"Total:"<<n<<endl;
  fin.close();
  fin.open("data.txt");
  double *A; //This is where I try to create dynamic array using the file. 
  A=new double[n];
  for (int i=0;i<n;i++)//I'm sure I'm doing this wrong can someone show me how
  {                    //how it done?
      while(fin)
      {
          cout<<A[i];
          fin>>A[i];
      }
  }
  fin.close();


}
This would be a perfect time to use a debugger!

do you need both loops for reading the file?
I'm not even sure how to create the dynamic array using the file. What I have posted starting from line 23 is how I thought I could do it, but to be honest I don't even know what I'm doing.

I believe you do need two loops for reading the file.

Up to line 22 everything runs fine and I see how I did it.

Then staring from line 22 I open up the file again I know I have to create a dynamic array so I create an address of A.

This is where I'm confused, would I set that A on line 24 =double[n]? I feel like it should be set = double[temp], because that's all the numbers in the file, n is just the total amount of numbers in the file.

From that point onward I don't even know what I was doing any help?
Your problem is not with the dynamic array, it is creating an array with the required length.

If you open this in a debugger and go through line by line you will see that in
1
2
3
4
5
6
7
8
for (int i=0;i<n;i++)
  {                    
      while(fin)
      {
          cout<<A[i];
          fin>>A[i];
      }
  }

The while loop is loading the file into A[], therefore the for loop is not needed. But if you remove the while loop and use the for loop, it will also work because you are loading the correct amount of items. You can pick either of these and they will work:
1
2
3
4
5
for (int i=0;i<n;i++)
  {                    
      cout<<A[i];
      fin>>A[i];
  }

1
2
3
4
5
6
7
int i;
while(fin)
{
    cout<<A[i];
    fin>>A[i];
    i++;
}


One more thing, because you are using new and creating a dynamic array, you must call delete or you will get a memory leak.

At the end of main() put this line
delete A;

If your program is crashing on line 22, you can try replacing
1
2
fin.close();
fin.open("data.txt");

with
1
2
fin.clear(); //clears end of file bit
fin.seekg(0, ios::beg); //goes back to the start of the file 

This may help because I have noticed in some OS, closing a file and opening it again too quickly will fail. I have noticed this in windows 8.
Last edited on
Okay, with some help from someone I was able to come up with this over the past day and It seems to run however now the total I cout in the beginning is not outputting and only Array A[i] is running. Is there a way to output both results? Also though A[i] is running and it is sorted, some of the numbers that were printed seem different from those in my file.

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    std::ifstream fin("data.txt");
    double temp;
    size_t n = 0;

    while(fin >> temp)
    {
        std::cout << temp<<" "<<endl;;
        ++n;
    }
    std::cout<<"Total:"<<n<<endl;

    fin.close();
    fin.open("data.txt");

    double *A = new double[n]();

    for (int i=0; i<n && std::cin >> A[i]; ++i);
    fin.close();

    for (int i=0;i<n-1;i++)
    {
        for (int j=i;j<n;j++)
        {
            if (A[i]>A[j])
            {
                int temp=A[i];
                A[i]=A[j];
                A[j]=temp;
            }
        }
    }

    for (int i=0;i<n;i++)
        cout<<"Array:"<<A[i]<<endl;
        cout<<"min:"<<A[0]<<endl;

    delete [] A;
}
Your problem is that you are trying to use std::cin, which is c input, instead of fin, which is your file.

There is another thing you can do if you want to be pedantic. In every for loop where you compare with size_t, make sure you are comparing with size_t instead of another type. This is because you don't know what type size_t is, it might be an unsigned int, or a long int.

You also mention that some numbers are changed, which numbers are changing?

Here is your code with both changes:

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    std::ifstream fin("data.txt");
    double temp;
    size_t n = 0;

    while(fin >> temp)
    {
        std::cout << temp<<" "<<endl;;
        ++n;
    }
    std::cout<<"Total:"<<n<<endl;

    fin.close();
    fin.open("data.txt");

    double *A = new double[n]();

    for (size_t i=0; i<n && fin >> A[i]; ++i);
    fin.close();

    for (size_t i=0;i<n-1;i++)
    {
        for (size_t j=i;j<n;j++)
        {
            if (A[i]>A[j])
            {
                int temp=A[i];
                A[i]=A[j];
                A[j]=temp;
            }
        }
    }

    for (size_t i=0;i<n;i++)
        cout<<"Array:"<<A[i]<<endl;
        cout<<"min:"<<A[0]<<endl;

    delete [] A;
}
Topic archived. No new replies allowed.