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;
}

Dani AI

Generated

Good quick thread — and spotted the filename/extension angle, @triumphoussain closed the loop by noticing the output file, and 's suggestion to try absolute paths is also useful. A few deeper, practical checks make these kinds of IO problems much easier to debug and avoid subtle bugs later.

Always check that formatted extraction actually succeeded and avoid using uninitialized variables. Formatted extraction into arithmetic types will set stream error bits on failure; a safe pattern is to open with RAII, zero-initialize the floats, then test the extraction in one step and bail out with a clear error message if it fails. Example pattern:

#include <fstream>
#include <iostream>

std::ifstream in("data.in");
if (!in) { std::cerr << "Cannot open input\n"; return 1; }

float a{}, b{}, c{}, d{};
if (!(in >> a >> b >> c >> d)) {
  std::cerr << "Read failed: wrong format or premature EOF\n";
  return 1;
}

The rules for formatted extraction and the failbit behavior are documented here. (en.cppreference.com)

Relative filenames are resolved against the process current working directory, so the file must live where the program is actually started. Printing the CWD at runtime is a fast way to confirm where to put your data file; in modern C++ use std::filesystem::current_path() for that. (en.cppreference.com) If you are running from Visual Studio, the debugger’s working directory defaults to the project configuration location and can be changed in the project Debugging settings. (learn.microsoft.com)

Small finishing tips: set output formatting (precision, showpoint) after you open the output stream so it’s obvious which stream is being configured, and always report errors to cerr with a helpful message before exiting. See the manipulators docs (showpoint, fixed, setprecision) and the ofstream reference for correct usage. (en.cppreference.com)

These checks (initialize variables, test extraction, confirm CWD, use absolute paths when needed) will catch most “nothing happens” cases quickly.

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.