ok so i have this program here i had to finish, and it was for input and output files, i wrote everything, and even wrote the data.in and data.out files but when i run it i get nothing in return. do the files have to be in the ".in" format cause when i save them, in notepad its data.in.txt

the only thing that was in data.in were four float values
but it returns blank, why? please help.

#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	float val1, val2, val3, val4;  //declares 4 variables
	ifstream inData;              // declares input stream
	ofstream outData;            // declares output stream

	outData << fixed << showpoint;

	inData.open("data.in");
	outData.open("data.out");

	inData >> val1 >> val2 >> val3 >> val4;
	outData << val1 << endl;
	outData << val2 << endl;
	outData << val3 << endl;
	outData << val4 << endl;

	inData.close();
	outData.close();

	system("pause");
	return 0;
}

Recommended Answers

All 4 Replies

ok so i have this program here i had to finish, and it was for input and output files, i wrote everything, and even wrote the data.in and data.out files but when i run it i get nothing in return. do the files have to be in the ".in" format cause when i save them, in notepad its data.in.txt

the only thing that was in data.in were four float values
but it returns blank, why? please help.

#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
	float val1, val2, val3, val4;  //declares 4 variables
	ifstream inData;              // declares input stream
	ofstream outData;            // declares output stream

	outData << fixed << showpoint;

	inData.open("data.in");
	outData.open("data.out");

	inData >> val1 >> val2 >> val3 >> val4;
	outData << val1 << endl;
	outData << val2 << endl;
	outData << val3 << endl;
	outData << val4 << endl;

	inData.close();
	outData.close();

	system("pause");
	return 0;
}

If the name of the file in windows is data.in.txt then you have to specify that name in the constructor for your ifstream object. So it should look like this instead:

inData.open("data.in.txt");

Also when opening files ALWAYS check to make sure they are open (i.e make sure you have received a valid file handle). Here are two examples:

// This uses the fstream method is_open() to make sure you have a valid file handle
if( inData.is_open() ) {/* Do something*/ }

// This example exits the program if there is no valid file handle
if(!inData) {/*Exit function or program*/}

Did you create data.in with notepad? If you did you have to set save as type to All Files, otherwise it automatically applies an extension of .txt to your file. Alternatively you could just change to

inData.open("data.in.txt")

and no, file types can have whatever extension you want them to have (or none).

yea nvm thank you, i was just stupid, and didn't realize it saved it into the data.out file, vs popping up in the command window haha. sorry for wasting your time

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.