Hi,
What I actually want to do is really quite simple,that is, convert all the data(of short type)in a file to their abstract values respectively.But I did not accomplish it. Here my code is listed:

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

using namespace std;

int main()
{
	char *in_name = "predicted_file.pre";
	char *out_name = "abstracted_file.pre";
	short i;
	short i_abs;
	ifstream in_file;
	in_file.open(in_name,ios::binary);
	ofstream out_file;
	out_file.open(out_name,ios::binary | ios::app);

	while (!in_file.eof())
	{
		in_file.read(reinterpret_cast<char*>(&i),sizeof(short));
		if (in_file.eof())
			break;
		i_abs = abs(i);
		out_file.write(reinterpret_cast<char*>(&i_abs),sizeof(short));

	}
	in_file.close();
	out_file.close();
	cout<<"Completed!"<<endl;
	return 0;
}

The program can be compiled without error but it seemed to be trapped in a dead loop(a time too long for me to be tolerant,exactly) and the output file turned out to be very large in stead of being the same size as input file when I terminated the program by force. Anybody can tell me where I made mistakes and give me some clue for a effctive solution?Thank u in advance.I use VS 2005 if that makes any difference.
nanchuangyeyu

Try to change your while loop to this:

while (in_file.read(reinterpret_cast<char*>(&i),sizeof(short)))
{
	i_abs = abs(i);
	out_file.write(reinterpret_cast<char*>(&i_abs),sizeof(short));
}

What is it: abstract values? May be you mean absolute value?
reinterpret_cast considered harmful ;)

typedef std::basic_ifstream<short> ishortints;
typedef std::basic_ofstream<short> oshortints;

void doIt(const char* source, const char* target)
{
    if (!source || !target)
        return;
    ishortints fraw(source,ios_base::binary);
    oshortints fabs(target,ios_base::binary|ios_base::app);
    short x;
    if (fraw && fabs)
    while (fraw.get(x)) {
        x = abs(x);
        fabs.put(x);
    }
}

BTW, Why is there actually need for the file being opened in binary mode? Why don't you just use the text mode ?

BTW, Why is there actually need for the file being opened in binary mode? Why don't you just use the text mode ?

Because the file apparently contains the binary (or computer internal storage) values of the data, which are not human-readable and not text. Such files can not be opened in text mode.

Because the file apparently contains the binary (or computer internal storage) values of the data, which are not human-readable and not text. Such files can not be opened in text mode.

Yeah, I know but I just find that a bit clumsy, if the numbers are stored a non-human-readable text you'll have to use a conversion program to insert data into a file, before you can use this file with this program :)

Edit:: I actually meant that you could also use the text mode for this purpose (reading some numbers from a file, take the absolute value from it and write it (=the absolute value) back to another file)

binary files have the advantage that they normally have fixed-length records and are faster read/write operations. All large databases use binary fines because of (1) speed, (2)random access, and (3) ability to change a record without rewriting the entire file (assuming fixed-length records). The disadvanges (1) non-human readable, so you can't use a text editor to view the file contents, (2) difficult to change record format (e.g. add or remove a field in the record)

Because the file apparently contains the binary (or computer internal storage) values of the data, which are not human-readable and not text. Such files can not be opened in text mode.

They can, but i/o methods may produce a rather strange results (to eat some bytes - all '\r', for example).

Feel the difference between external files (independent objects in the outer world) and C++ streams (temporary program artifacts). C++ fstreams are external file accessors only. Moreover, it's class fstreambuf role, class fstream does not know where data exist.

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.