Sign in to follow this  

writing char(10) to a file

This topic is 4854 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'm tryig to write to a binary file (A map file) and when I try to write a byte with the value of 10, it makes two of them. Is there a way to disable this? I'm using Windows XP and MSVC++ 6, btw. here is the code that i'm using (i'm just trying to make a usable map to test my tile engine with, so it's kind of a quick hack for a map editor)
#include <fstream.h>

void test();
void main()
{
	ofstream out;
	out.open("house.map", ios::out);
	int x = 0;

	for(x = 0; x < 9; x ++)
	{
		out << char(10);
	}
// there's more output in the actual code

	out.close();
	test();
}

void test()
{
	char map [100];//a real map would be much larger, of course
	ifstream read;
	read.open("house.map", ios::in || ios::nocreate);
	int filesize = read.tellg();
	read.seekg (0, ios::beg);
	for(int x = 0; x < 100; x ++)
	{
		read.seekg(x);
// i add 100 to the value before i output it so that the characters are recognizable
		map[x] =  char(read.peek() + 100);
	}
	read.close();
// this is my output loop (this example map is 8 tiles wide
	for(int y = 0; y < 8; y ++)
	{
		for(int z = 0; z < 8; z ++)
		{
			cout << map[8 * y + z];
		}
		cout << endl;
	}
}

Share this post


Link to post
Share on other sites
You're using text mode. As a result, when you try to write the newline character '\n' (10) it also adds the carriage-return character '\r' (13). You need to open the file in binary mode. I don't know the STL way of doing this though.

Share this post


Link to post
Share on other sites
You have to open the file in binary mode, otherwise ofstream will do some conversions.

out.open("house.map", ios::out|ios::binary);

You might also want to use out.put(char(10)) instead of << since << is for formatted output.

Share this post


Link to post
Share on other sites
I recently had this problem, using c++, I managed it by doing this:


#include <stdio.h>

int main()
{
FILE *file = fopen(filename,"wb");
GLubyte temp = ...
fwrite(temp,1,1,file);
}


that is it roughly, i worked it out after doing a bit of experimenting.

Share this post


Link to post
Share on other sites

This topic is 4854 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this