Jump to content
  • Advertisement
Sign in to follow this  
v0dKA

std::ofstream Doesn't Work [FIXED]

This topic is 5036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I made a simple app that dumps all files in a directory to a text file. I'm having trouble with std::ofstream - the compiler complains that it uses an undefined class. The app is pretty short, so I'll post the entire thing:
#include <windows.h>
#include <iostream>
#include <string>
#include <fstream.h>

int main()
{
	// CHANGE THESE TO CHANGE SEARCH PATH AND DUMP PATH
	const char SEARCH_PATH[ _MAX_PATH ] = "C:\\Program Files";
	const char DUMP_FILENAME[ _MAX_PATH ] = "C:\\Documents and Settings\\Owner\\Desktop\\Dump.txt";

	std::cout << "Initializing...\nSearch Directory: " << SEARCH_PATH
		<< "\nDump Filename: " << DUMP_FILENAME << "\n";

	WIN32_FIND_DATA Data;
	char search_path[ _MAX_PATH ];	// Define temporary path to work with (non-const)
	wsprintf( search_path, "%s%s", SEARCH_PATH, "\\*.*" );

	HANDLE handle = FindFirstFile( search_path, &Data );
	if( handle == INVALID_HANDLE_VALUE )
	{
		std::cout << "\nError! Invalid handle value after attempting to find first file.
\nPlease check the search directory exists\n";
		system( "Pause" );
		exit( 1 );
	}

	std::string _full;	// Temporary buffer for full filename
	std::string _short;	// Temporary buffer for short filename (no path)
/***************************** ERROR next line ***********************/
	std::ofstream dump_file;	// Dump files and dirs
	dump_file.open( DUMP_FILENAME );

	dump_file << Data.cFileName << "\n";

	std::cout << "\nBeginning dump:\n" << Data.cFileName << "\n";
	while( FindNextFile( handle, &Data ) )
	{
		dump_file << Data.cFileName << "\n";
		std::cout << Data.cFileName << "\n";
	}

	FindClose( handle );

	std::cout << "\nFiles dumped successfully.\n";
	system( "Pause" );

	return 0;
}




Errors: error C2079: 'dump_file' uses undefined class 'basic_ofstream<char,struct std::char_traits<char> >' The error points to the line that I commented as ERROR. There are other errors, but they are insignificant because they're all due to this error. Anyone know what's wrong?

Share this post


Link to post
Share on other sites
Advertisement
Don't mix <iostream> and <fstream.h>. For that matter, just don't use <fstream.h> at all. The proper header is <fstream> without the .h.

Share this post


Link to post
Share on other sites
Ah, fixed. Replaced <fstream.h> with <fstream>. That still caused errors (cout is not a member of std), so I re-included iostream.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!