Jump to content
  • Advertisement
Sign in to follow this  
Shnoutz

Loading a text file as binary

This topic is 3808 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

Hello, I feel stoopid... I am trying to load a text file as a binary file into a buffer. so I made the following classes :
	class File : public Void
	{
	public:

		inline File(FILE * const p_file) : m_file(p_file)
		{
			assert(p_file);
		}

		virtual ~File()
		{
			fclose(m_file);
		}

		size_t const size() const
		{
			long const position = ftell(m_file);
			fseek(m_file, 0, SEEK_END);
			long const length = ftell(m_file);
			fseek(m_file, position, SEEK_SET);
			return length;

		}

	protected:

		FILE * const get() const
		{
			return m_file;
		}

	private:

		FILE * const m_file;
	};

	class InputFile : public File
	{
	public:

		InputFile(char const * const p_path) : File(fopen(p_path, "r))
		{
		}

		template < typename Allocator >
		void read(ArrayTemplate < unsigned char, Allocator > & p_buffer, size_t const p_offset, size_t const p_size)
		{
			size_t const fileSize = size();
			if(p_offset < fileSize)
			{
				size_t const bufferSize = min(fileSize - p_offset, p_size);
				if(bufferSize)
				{
					p_buffer.dim(bufferSize);
					fseek(get(), (long) p_offset, SEEK_SET);
					fread(p_buffer.data(), bufferSize, 1, get());
				}
			}
		}
	};
but when I load a file that looks like this :
package Demo
{
	import System
	
	class DemoApp implements System.MainThread
	{	
		public void main()
		{
			System.print("Hello world")
		}
	}
}
my buffer contains something like this :
package Demo
{
        import System

        class DemoApp implements System.MainThread
        {
                public void main()
                {
                        System.print(Hello world)
                }
        }
}════════════════════════
notice the undesired "════════════════════════" at the end :( do you see something wrong with my thing?

Share this post


Link to post
Share on other sites
Advertisement
Never mind, I am a total noob.

I needed to specify binary format "rb" not just "r". Using "r" alone defaults to "rt" and my goal was to open the text file as a binary file.

Share this post


Link to post
Share on other sites
What is wrong with std::ifstream and std::vector? And what the hell is Void?

assert() is unsuitable for runtime errors. It should be reserved for detecting logic errors - failing to open a file is not a logic error.

As is, File does not implement the rule of three.

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!