I m surprised and wanna know that how the compiler understand the sequence of how data was entered. as i didnt use any space any tab not a new line, how it can point to the original value from a
single line..?????????

find.txt contain the following line with out spaces with out tabs with out new line.

find.txt
123433344566786804321235667

Below is the C++ code.

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
	int arr[10];
	ofstream out;
	 /////////////////////////////////////////// Use for writing array of integer in file
	out.open("find.txt",ios::out);
	for(int i =0;i<10;i++){
		cout << "Enter Number for Input in file ";
		cin >> arr[i];
		out << arr[i];
	}
	out.close();
	
	cout << "------------------------------File contents------------------------------" << endl;
	ifstream in;
	in.open("find.txt",ios::in);
	int count=0;

	while(in.eof()==0){
		in >> arr[count++];
	}

	for(int i =0;i<10;i++){
		cout << "Data: " << arr[i] << endl;
	}

	in.close();
	return 0;
}

output of the program:

Enter Number for Input in file 123
Enter Number for Input in file 4333
Enter Number for Input in file 44
Enter Number for Input in file 5
Enter Number for Input in file 6678
Enter Number for Input in file 680
Enter Number for Input in file 432
Enter Number for Input in file 1
Enter Number for Input in file 23
Enter Number for Input in file 5667
------------------------------File contents------------------------------
Data: 123
Data: 4333
Data: 44
Data: 5
Data: 6678
Data: 680
Data: 432
Data: 1
Data: 23
Data: 5667
Press any key to continue . . .

Dani AI

Generated

Short answer: the formatted extraction operator (operator>>) doesn’t magically recover the original input boundaries — it reads as many digit characters as it can for a single integer, so a file that contains the numbers concatenated with no separators becomes a long run of digits that the extractor will try to interpret as one number (or fail if it doesn’t fit). That’s why the read-back in ’s example behaves unexpectedly: once extraction fails or consumes unexpected characters, the remaining array elements can remain holding the old values entered from the keyboard. was right to point out the root cause, but the practical fixes and safe patterns are worth spelling out.

Write numbers with an explicit separator (space or newline) and read while checking the stream state. Example patterns (not the original program):

Write with separators:

for (int i = 0; i < 10; ++i)
    out << arr[i] << '\n';

Read safely:

int i = 0;
while (i < 10 && in >> arr[i])
    ++i;

Don’t use while(!in.eof()) as the loop condition — check the extraction itself (in >> value) so you don’t process the results of a failed read. Also guard against overflow: if the concatenated digits exceed the range of int, formatted extraction will set the stream error state and further reads will fail.

If you need byte-accurate recovery (no separators allowed), use a binary format so byte boundaries are preserved:

out.write(reinterpret_cast<const char*>(arr), sizeof arr);
in.read(reinterpret_cast<char*>(arr), sizeof arr);

(remember binary is platform-dependent for endianness/size).

Troubleshooting checklist: open the file in a text editor to confirm separators; print in.fail() / in.eof() after a failed read to see why it stopped; prefer std::vector<int> when the count is unknown.

I m surprised and wanna know that how the compiler understand the sequence of how data was entered. as i didnt use any space any tab not a new line, how it can point to the original value from a
single line..?????????

The compiler has no clue how to interpret those digits. The only way it will work is if you write a space, tab, or some other character that acts as a separator.

It only appears to work -- that read loop didn't actually do anything because there was probably an integer overflow error on the first read. What you saw on the screen was the old data left in the array after keyboard input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.