The last number in my input file replaces all the numbers in my array.

When I assign a number for a section of an array it works.I know this because I output the data and it outputs what I have in my input.txt file.However, when I output it outside the while loop the first the number is the last number in my notepad file.

I have this in my input.txt

8
3
7

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
 
// Hello World with f.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	double instances = 0;
	
	double myArray[1];

	string line;
	ifstream myfile("input.txt", ios::out | ios::app | ios::binary);
	if (myfile.is_open())
	{
		while (getline(myfile, line))
		{
			
			instances = atof(line.c_str());
			
			for (int n = 0; n < 1; n++) {
				myArray[n] = instances;
				cout <<  myArray[n] << " ";
			}
			

		}
		myfile.close();
	}

	else cout << "Unable to open file";

	cout << "The first number is " << myArray[0] << '\n' ;
	cout << "The sec number is " << myArray[1] << '\n';
	cout << "The third number is " << myArray[2] << '\n';
			
	cin.get();


}





8 3 7 The first number is 7
The sec number is 2.49782e-314
The third number is 1.79773e-317

The weird numbers I assume are memory addresses.Been pulling my hair out on this.

Last edited on
I see several problems.

First you have an array that has room for one and only one element, so what is the point of the array?

Second you file contains numbers, why are you using a string instead of a numeric type?

Third you're accessing your array out of bounds which leads to undefined behavior.

Fourth why are you opening the file in binary mode? You're using formatted input methods so you should be opening the file as a "text" file not binary.

And why are you trying force an ifstream to be an output stream (an ifstream is always an input stream)?



I added size to my array. What do you mean when you say I am accessing my array out of bounds? How is the 7 going through if it is out of bounds? How do I go about fixing it?

Edit: i thought you need it to be ifstream to read and write in it.
Last edited on
What do you mean when you say I am accessing my array out of bounds?

You have an array of size 1, so the only valid index value would be 0, anything else would be trying to access your array out of bounds, which you were doing on lines 39 and 40.

How do I go about fixing it?

I recommend you use a std::vector instead of the array and then push_back() the values into your vector. And then when you want to display the vector contents use the size() of the vector to limit your loop.

Edit: i thought you need it to be ifstream to read and write in it.

An ifstream is in input stream, always. An ofstream is an output stream, always. A fstream can be used for both input and output when used properly. Since you're only reading from your file all you need is to use an ifstream with the default open modes. std::ifstream myfile("input.txt");


Topic archived. No new replies allowed.