Link to home
Start Free TrialLog in
Avatar of dandeliondream
dandeliondreamFlag for Singapore

asked on

fstream cannot output file

Hi,

I am trying to output data to a file but i get an empty output file. Please help.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MAX = 100;
const int maxSize = 20;
 
int constructInt(fstream&);
void subtract(fstream&, ofstream&, int []);
 
int main()
{
	srand(time(NULL));
	int n[maxSize];
	int n2[maxSize];
	
	fstream file;
	file.open("int_dataA.txt", ios :: out); //output to file
	
	if (!file) // file not there or (file.fail())
	{
		cout << "File could not be found.";
		exit(1);
	}
	
	fstream file2;
	file2.open("int_dataB.txt", ios :: out);//output to file
	
	if (!file2) // file not there or (file.fail())
	{
		cout << "File could not be found.";
		exit(1);
	}
	
	int size;
	size = constructInt(file); 
	cout << "The size of the random no list is: " << size << endl;
	
	ofstream outfile; //output to file
	outfile.open ("sub_data.txt");
	
	subtract(file, outfile, n);
	
	for (int i = 0; i < maxSize; i++)
		cout << n[i] << " " ;
 
	return 0;
}
 
 
int constructInt(fstream& x)
{
	int size, n;
	//size = 20;
	size = rand() % maxSize + 1; //size can be between 1 and 20
	
	for ( int i = 0; i < size; i++)
	{
		n = rand()% MAX + 1; //random no from 1 to 100
		x << n << endl;	  
	} 
	return size;
	x.close();
}
 
void subtract(fstream& file, ofstream& outfile, int n[])
{
	file.open("int_dataA.txt", ios :: in);
	
	int num, i = 0;
	
	while (file >> num)
	{	
 
		n[i] = num;
		i++;
	}
	
	for (int k = 0; k < i; k++)
		outfile << n[k];
			
	file.close();
	outfile.close();
}

Open in new window

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

You don 't perform any error handling on your streams, is_open(), eof(), bad(), fail() so how do you know if your reading or writing is working? I suspect you have a stream failure somewhere and it's just being ignored so the end result is nothing is written out.

http://www.cplusplus.com/reference/iostream/fstream/

Try adding some error handling and see what, if any, read/write errors you are getting as this will likely lead to the solution to your problem.
Sorry, you are checking for the file being opened (just spotted if(!file) ) but none of the reads or writes are validated from then on. You should check the stream state, strategically, after each read/write.
In constructInt you write to x, a file stream. You call this with a stream called 'file' but you open 'file' with ios::out so it's write only, no?
You know, to avoid confusion, rather than using fstream for everything with std::ios:: and/or std::ios::in you should, where possible, stick to using ifstream and ofstream as seperate interfaces as these two can't be mixed up when passing them around. If you pass everything as an fstream the C++ type system can't help you spot semantic issues where you have mixed up input for output and vice-versa.
Avatar of dandeliondream

ASKER

Hi evilrix,
we have to include fstream in our codes..no choice. I've revised my codes. The output file is not empty but showing the wrong data.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
using namespace std;
const int MAX = 100;
const int maxSize = 20;
 
int constructInt(fstream&);
void subtract(fstream&, ofstream&, int []);
 
int main()
{
	srand(time(NULL));
	int n[maxSize];
	int n2[maxSize];
	
	fstream file;
	file.open("int_dataA.txt", ios :: out); //output to file
	
	if (!file) // file not there or (file.fail())
	{
		cout << "File could not be found.";
		exit(1);
	}
	
	int size;
	size = constructInt(file); 
	cout << "The size of the random no list is: " << size << endl;
	
	
	
	ofstream outfile; //output to file
	outfile.open ("sub_data.txt");
	subtract(file, outfile, n);
	
	
	//for (int i = 0; i < maxSize; i++)
		//cout << n[i] << " " ;
 
	return 0;
}
 
 
int constructInt(fstream& x)
{
	int size, n;
	//size = 20;
	size = rand() % maxSize + 1; //size can be between 1 and 20
	
	for ( int i = 0; i < size; i++)
	{
		n = rand()% MAX + 1; //random no from 1 to 100
		x << n << endl;	  
	} 
	return size;
	x.close();
}
 
void subtract(fstream& file, ofstream& outfile, int n[])
{
	file.open("int_dataA.txt", ios :: in);
	
	int num, i = 0;
	bool badRead = 0;
	
	while (!file.eof())
	{
		do
		{
			file >> num;
			if (!file.eof() && file.fail())
			{
				badRead = 1;
				file.clear();
				file.ignore(2000,'\n');
			}
			else
				badRead = 0;
			
		}while (badRead);
		
		outfile << num << endl;
	};
	/*
	while (file >> num)
	{	
 
		n[i] = num;
		i++;
	}
	*/
	
			
	file.close();
	outfile.close();
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks for your help once again...
Did my initial assistance not lead you to the resolution of this problem? There were many other problems besides the one pointed out by I8!
Also, it is bad design to close the file in a different function to which you created/opened it. Did you actually solve this? If so, what was the solution?
>> There were many other problems besides the one pointed out by I8!

I agree, but I think it was the final change needed to get it working. Since the file was opened when it wasn't closed yet, there might have been an issue with buffering.
>> but I think it was the final change needed to get it working

And I assume that's why I got awarded all points.

dandeliondream, it's your choice, but usually, if more than one expert contributes to the solution/answer, the points are split among them.