Hi

I am making a program that reads a text file & displays them into an array.

My problem is; I read the text file to determine how big the array needs to be, then I clear the input from the text file, then I read the text file again & store the text/integers into an array once i know how big the array needs to be. The problem is clearing the input from the text file.

I have used the functions infile.clear(), infile.putback(x), infile.peek() and all of these dont work. Can you tell me how to get infile.clear() to work?

To better understand my problem I have the code below & the contents of the input file(number_list.txt):

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
	int x = 0, y = 0;
	int sequence_length = 0;
	double number_array[100];
	int size = 0;

	ifstream infile;

	infile.open("number_list.txt");

	if (!infile)
	{
		cout << "Failed to open file";
		return 0;
	}

	// determine the length of the array needed
	while(infile) {
		infile >> x;
		sequence_length++;
	}


	cout << sequence_length << endl;

	// clear the input from infile so we can store x into an array
	infile.clear();

	// store numbers in an array
	for ( int i = 0; i <= sequence_length; i++)
	{
		infile >> x;
		number_array[i] = x;
	}
	
	infile.close();

	for (int i = 0; i < sequence_length; i++)
	{
		cout << number_array[i] << ",  ";
	}

	return 0;
}

input file contents:

20 40 45 231 121 45 90 43 -10 -40 -70

sample of my output:

12
-70, -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, -70,

Recommended Answers

All 4 Replies

To check the length of file you have done

while(infile) {
		infile >> x;
		sequence_length++;
         }

After this the file pointer is at the end of file now you need to re-assign the pointer to the start of file.So before taking array input do

infile.seekg(0,ios::beg);

And then take the input into the array. And ya do not clear the file because you cannot fetch values into the array if you do so.

And also make following changes :

int sequence_length = -1;
// store numbers in an array
for ( int i = 0; i < sequence_length; i++)
{
	infile >> x;
	number_array[i] = x;
}

I put "infile.seekg(0,ios::beg);" into my code but the output is the same as before:

output:

12
-70, -70, -70, -70, -70, -70, -70, -70, -70, -70, -70, -70,

Any advice? Thanks for the help so far also :)

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;

int main()
{
	int x = 0, y = 0;
	int sequence_length = -1;
	double number_array[100];
	int size = 0;

	ifstream infile;

	infile.open("number_list.txt");

	if (!infile)
	{
		cout << "Failed to open file";
		return 0;
	}

	// determine the length of the array needed
	while(infile) {
		infile >> x;
		sequence_length++;
	}


	cout << sequence_length << endl;


	infile.seekg(0,ios::beg);

	// store numbers in an array
	for ( int i = 0; i <= sequence_length; i++)
	{
		infile >> x;
		number_array[i] = x;
	}

	infile.close();

	for (int i = 0; i < sequence_length; i++)
	{
		cout << number_array[i] << ",  ";
	}

	return 0;
}

I told you not to delete the contents of the file thats it. But I didn't suggest to remove infile.clear() because its use here is to reset all the flags related to ifstream.
Therefore:

infile.clear();
infile.seekg(0,ios::beg);

Will do your work.

And also you haven't corrected this:

// store numbers in an array
for ( int i = 0; i <= sequence_length; i++)
{
infile >> x;
number_array = x;
}

It should just be < not <=.

Why are you bothering to count the number of integers in the file before actually reading them into the array? Your program doesn't do any dynamic allocation of the array, so what's the point of doing that? Just put the data in the array the first time the file is read and be done with it.

sequence_length = 0;
while( sequence_length < 100 && infile >> number_array[ sequence_length++]) 
{
         // do nothing
}
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.