Hey, I have started working with I/O file streams using my 'problem solving with c++' book, I am not sure where the .dat file goes in MS Visual C++ or how to make it for that matter...

Also is this code correct? I am getting strange errors...

I am trying to output the largest number from a file to the screen.

#include "stdafx.h"
#include <fstream> // file stream
#include <iostream> // for cout
#include <cstdlib>// exit

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	ifstream in_stream;     // input file stream
	//ofstream out_stream;
	in_stream.open("infile.dat"); // open infile.dat file

	if (in_stream.fail( ))
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}

	//out_stream.open("outfile.dat")
	int res;
	int num1, num2, num3;
	in_stream >> num1 >> num2 >> num3; // each line is read from the file
	cout << "The largest number in the infile.dat file is: ";
	cout << max(num1, num2, num3); // the biggest number is calculated and output;
    
	res = num1 + num2 + num3;
	cout << res;
	cout << endl;
    in_stream.close();

	return 0;
}

Thank you for any help!

Recommended Answers

All 6 Replies

> Also is this code correct? I am getting strange errors...
Whilst saying that they're "strange" errors is a step up on "I'm getting errors", it really doesn't help us that much to figure out what's going on.

Be specific, post actual error messages and actual observations.

The .dat file extension usually refers to files that contain binary information as opposed to text files. So if you want to read a binary file you have to open it with ios::binary flag as the second argument to the open() function. If you don't do that and if you are using MS-Windows operating system the stream object may mis-interpret any bytes that are the same as the '\n' line terminator. *nix and MAC don't have that problem.

The second thing about binary files is that the >> extraction operator don't work. That is for text files, not finary files. You have to use the stream's read() method.

The easiest way to determine if a file is binary or not is open it with Notpad. If you see a bunch of unreadable garbage such as small squares, then the file is binary. If everything can be read normally then it is a text file.

Generally, if you are running your program from within the Visual C++ (Visual Studio) environment (using the Debug menu), your input/output files will be in the project folder - the same place you find your .cpp file. If you try to run the executable directly (in a command window or by double clicking the .exe file) you are probably in the \Debug folder under your project folder - that's where the program will look for the input file.

You can put the dat file in the same directory from where you are running the program, else specify full path like C:\\ ,etc.

Hey again, the .dat file is not to contain binary, it is just to contain numbers on separate lines.

at the moment infile.dat contains:

1
9
4

This is the errors I get:

1>C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(290) : error C2064: term does not evaluate to a function taking 2 arguments
1>        C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(2946) : see reference to function template instantiation 'bool std::_Debug_lt_pred<_Pr,_Ty,_Ty>(_Pr,const _Ty1 &,const _Ty2 &,const wchar_t *,unsigned int)' being compiled
1>        with
1>        [
1>            _Pr=int,
1>            _Ty=int,
1>            _Ty1=int,
1>            _Ty2=int
1>        ]
1>        .\largestandsmallest.cpp(28) : see reference to function template instantiation 'const _Ty &std::max<int,int>(const _Ty &,const _Ty &,_Pr)' being compiled
1>        with
1>        [
1>            _Ty=int,
1>            _Pr=int
1>        ]
1>C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(292) : error C2064: term does not evaluate to a function taking 2 arguments

> max(num1, num2, num3);
max only takes 2 params
Perhaps max(num1, max(num2, num3) );

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.