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*/}