Public Group

# writing char(10) to a file

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

## 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
for(int x = 0; x < 100; x ++)
{
// i add 100 to the value before i output it so that the characters are recognizable
}
// 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 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 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 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.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632154
• Total Posts
3004472

×