954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

file input and output help please

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;
}
fabricetoussain
Newbie Poster
12 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

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*/}
hag++
Junior Poster
197 posts since Jan 2010
Reputation Points: 34
Solved Threads: 31
 

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).

burgercho
Light Poster
47 posts since Jul 2008
Reputation Points: 26
Solved Threads: 11
 

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

fabricetoussain
Newbie Poster
12 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Uhh Try putting a location in the program like C:/Users/fdnsdnlgds/data.in

as for why its not grabbing the data... I dont know.. I usually use getline();
Used to have these problems.. try checking this thread:

http://www.daniweb.com/forums/thread283874.html

triumphost
Posting Whiz
390 posts since Oct 2009
Reputation Points: 57
Solved Threads: 36
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You