• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

## Recommended Posts

##### Share on other sites
Your file is straight ASCII text. When you read it, it is assumed you are going to be grabbing an entire character at a time.

The reason most other files look like hieroglyphics to us is because the data is designed to be read in terms of bytes. The string "32" is enormously inefficient compared to how many bits you really need to represent that number.

If you want a good example of why ASCII text files aren''t used by many programs, look at the difference between a BMP and a PGM file (a PGM image is saved as an ASCII file and is very easy to read by a human). I just loaded up one of my image-processing programs and saved the same exact image as a BMP and then as a PGM. The BMP weighed 576KB, and the PGM clocked in at 913KB!

##### Share on other sites
That clears that up. I knew there was some reason why using regular text files isn''t the way it''s done...

Ok, so knowing a bit more then I did before, I want my program to create and read files that are read in bytes.

For now I won''t be wanting to do anything as complex (in my mind) as importing a .3ds file, but instead take the program I have now, think of it as a 3d modeler, and tell to take all the data (right now just vertices xyz locations and the center point of an object) and save it in a .MCE (My Cool Extension) file. When I open it in notepad, it should like like this:

>¢~0<ýÊS½oæ>áú˜;ýÊS½oæ>áú˜;ÖŸS½+sß>ú1?
<ÖŸS½+sß>ú1?<ÉÖR½­Þ>‘;<ÉÖR½­Þ>‘;<Á÷P½rÀÝ>

Here are some questions:

1) How do I do that
2) Are c++ fstream functions used?
3) Using what ever method to write to a file, would one simply pass pointers adresses to the file?
4) Could you or someone post an example of a really simple file write function, maybe something like this:

const char *text = "Hello";
.
.
.
// (some code putting ''text'' into a file)

and have the contents of the file, when looked at in notepad look like this:

-©2L½C¡Ü>ÿ_ (...thats the most important part to me, is how it looks )

5) What are the prerequisites or programming skills needed before getting into all of this?

##### Share on other sites
You could try this code, ( I haven''t tested it )

  #include using namespace std;void main(){ int number; int numberArray[10]; ofstream outfile("test.dat", ios::out | ios::binary); ifstream infile("test.dat", ios::in | ios::binary); // Write the integers 1-10 to the file for(int i = 1; i <= 10; ++i) outfile.write((const char*)&i, sizeof(i)); // Close the out file outfile.close(); // Read back all numbers and store them in an array infile.read((char *)&numberArray, sizeof(int)*10); // testing.... cout << numberArray[5]; // If you want to read back the values one-by-one use // this instead // while(infile.read((char*)&number, sizeof(number))) // cout << number << endl; // Close the infile infile.close();}

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627655
• Total Posts
2978458

• 10
• 12
• 22
• 13
• 33