Writing on a file problem!

Hi!
I want to create a program which writes on a file an array of integers and then returns the highest value of this array. I'm showing you the code , CAN SOMEBODY TELL ME WHERE IS THE PROBLEM?
Thank you!
P.S:: I expect to it to return 4 but when i run it it returns -5.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int read_data(fstream& in)
{
int highest;
int next;
if(in>>next)
highest=next;
else
return -5;
while(in>>next)
{
if(next>highest){
highest=next;
}
}
return highest;
}


int main()
{
fstream data;
string filename;
cout<<"Please enter a filename"<<endl;
cin>>filename;
int matrice[4]={1,2,3,4};
data.open(filename , ios::out);
for( int i=0; i<4; i++)
{
data<<matrice[i]<<endl;
cout<<matrice[i]<<endl;
}
if(data.fail())
{
cout<<"Error opening"<<data<<"\n";
return 1;
}

cout<<"The highest value is: "<<read_data(data);
data.close();

return 0;
}
Last edited on
Use ofstream to write to a file, and ifstream to read from file.
I just did it but it shows me this error:

Error 1 error C2664: 'read_data' : cannot convert parameter 1 from 'std::ofstream' to 'std::ifstream &
Use ofstream to write to a file, and ifstream to read from file.

You can use fstream to do both.

A few things, Alendrex. You need to set up your fstream to work with both the input and output stream:
 
data.open(filename.c_str(), ios::out | ios::in); // in main 


Because you'll be using it for input in your read_data function.

The reason why your returning -5 all the time is because your file stream is at the end of the file since it just finished writing to it and needs to be set back to the beginning, so do this:
 
in.seekg(ios::beg); // in read_data 


Lastly, fstream::fail() doesn't specifically error for opening the file. You'll want to use fstream::is_open() and you'll want to check that before you try writing to it.
Last edited on
You need to set up your fstream to work with both the input and output stream

If you leave out that parameter it does that by default...

your file stream is at the end of the file since it just finished writing to it and needs to be set back to the beginning

The istream and ostream pointers are independent of each other in my testing.
If you leave out that parameter it does that by default...

Ah, didn't realize that.

The istream and ostream pointers are independent of each other in my testing.


Copying Alendrex's code and accounting for both input and output, I get the same exact result as him until I reset to the beginning of the file. *messes with fstream some more*
Last edited on
Yeah, after some testing with this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	fstream data;

	data.open("aFile.txt");

	if (data.is_open())
	{
		cout << data.tellg() << endl;
		cout << data.tellp() << endl;

		for (int i = 0; i < 8; i++)
		{
			data << i << endl;
			cout << data.tellg() << endl;
			cout << data.tellp() << endl;
		}
	}

	data.close();
	return 0;
}


The ofstream and ifstream pointers are incrementing in unison for me.
Last edited on
Huh, I didn't know that, sorry. I guess my testing was different somehow...I'll jave to do some more testing to determine what I originally did...look at me still talking when there's testing to do.
Last edited on
cout << data.tellg() << endl;
cout << data.tellp() << endl;

What are they for???
Topic archived. No new replies allowed.