mfc >> How to write a binary file

by terrcy » Mon, 26 Apr 2004 11:01:08 GMT

TO all:

here ,I have a struct.

struct test{
char xx;
char *yy;
}

Now ,How can I write it into a binary file?

ths for anyone reply.




mfc >> How to write a binary file

by Balboos » Mon, 26 Apr 2004 13:42:37 GMT


Open the target file in 'binary' mode. But, exactly what you want to
write to the file is what makes a difference.

Writing char strings will look the same (although translation of '\n'
(will not occur). If you have numeric data, however, this could be
quite different. If your char/strings contain embedded null's, then you
would need to recast them prior to the write (or it will stop at the
null char).

In text ouput, the numerical vals are 'translated' to their ascii char
representation, while in binary, they're not - again, assuming you use
typical formatting procedures.

Do you plan on writing out the address of .yy ? It would normally be
pretty useless, except for diagnostics.

Also, unless you instruct the compiler otherwise, the length of your
strut (via sizeof) will not be equal to the length a char + a char *, as
structures are filled out to a certain length/member (allignment).

i.e.: sizeof (struct ..)!= sizeof(char) + sizeof(char *)

unless you use #pragma pack 1

You may wish to explain what it is you wish to do (and why).


Balboos

mfc >> How to write a binary file

by Igor Green » Mon, 26 Apr 2004 14:00:19 GMT

> struct test{

Do you want to save string that yy is pointing to? In this case it should be
something like this (error checking is skipped):

CFile file;
// ------- writing
file.Open(sFileName, CFile::modeWrite|cFile::modeCreate);
file.Write(&test.xx, 1);
WORD wLen = 0;
if (test.yy)
wLen = strlen(test.yy);
file.Write(&wLen, sizeof(wLen)); // save length first
if (test.yy)
file.Write(test.yy, wLen);
// --------- reading
file.Open(sFileName, CFile::modeRead);
file.Read(&test.xx, 1);
WORD wLen = 0;
file.Read(&wLen, 2);
if (wLen)
{
test.yy = new char[wLen+1];
file.Read(test.yy, wLen);
test.yy[wLen]=0;
}
else
test.yy=NULL;

mfc >> How to write a binary file

by terrcy » Mon, 26 Apr 2004 14:11:28 GMT

ths

Similar Threads

1. reading and writing to binary files - CSharp/C#

2. How to write a binary file

TO all:

here ,I have a struct.

struct test{
  char xx;
  char *yy;
}

Now ,How can I write it into a binary file?




3. 0D after 0A in hex when writing a binary file

4. Reading and Writing to Binary Files

I'm working on a program that must first establish if the file exists in the
program directory then it must open if for reading, read each line and set
the variables then the program goes on about it's buisness.

My problem is all the resources I have found aren't very clear on these
things. All of them open the file then check to see if the stream is open.
Well, the problem with using file.open("filename.dat", ios::in |
ios::binary) is that if the file doesn't exists, it creates it
automatically. I need to see if the file exists, if it doesn't show an error
message, then if the file does exists, open if, read from it, then set the
global program variables. Below is my code so far to try to get this work.

I've marked where I needed help with // Need  help here and many question
marks
Hopefully you guys will get a good view of what i'm doing, just a basic
console app in MSVC++ that pulls information in a binary file then if that
is successful allow you to set the variables to something else. Then run the
program again and view the information then set it to something different.

===== MAIN.CPP =====
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>

// variables
int Box_X;
int Box_Y;
int Box_Z;
float input_X;
float input_Y;
float input_Z;
char* filename = {"main.dat"};

// Functions
bool pullconfig();
bool setconfig();

// main function
int main(){
    cout << "Pulling configuration information...\n";
    if(pullconfig() == false){
        cout << "\nError opening \"main.dat\" file: Does Not Exist!\n";
        return 0;
    }
    cout << "Configuration pulled successfully!\n";
    cout << "\nBox_X = " << Box_X << "; Box_Y = " << Box_Y << "; Box_Z = "
<< Box_Z << "\n";
    cout << "Set Box_X to: ";
    cin >> input_X;
    cout << "Set Box_Y to: ";
    cin >> input_Y;
    cout << "Set Box_Z to: ";
    cin >> input_Z;
    if(setconfig() == false){
        cout << "\nError setting configuration information!\n";
        return 0;
    }
    cout << "\nNew Configuration Set Successfully!";
    return 0;
}

bool pullconfig(){
    ifstream fin(filename, ios::in | ios::binary);
    if(!fin.is_open()){
        return(false);
    }

    // Need Help here for pulling info and setting it to Box_X, Box_Y, and
Box_Z ????????????????????????????????
    return(true);
}

bool setconfig(){
    ofstream fout(filename, ios::out | ios:binary);
    if(!fout.is_open()){
        return(false);
    }
    fout << "Box_X = " << input_X << "\n";
    fout << "Box_Y = " << input_Y << "\n";
    fout << "Box_Z = " << input_Z << "\n";
    fout.close();
    return(true);
}



5. writing binary file (ios::binary)

6. Writing a structure to a file (writing it as binary data)

Here's a really stupid question - I have a class that I want to write to a 
file to retrieve later (I've already made the class serialable ). How do you 
do that? I can't find anything that will let me write out an entire class at 
once?

TIA - Jeff.


7. Binary file writing problem - CSharp/C#

8. Convert binary file->utf8->binary file

Hi,
Does anyone know how I am able to write a utf-8 encoded binary string into 
binary file?
Currently I am given a UTF-8 string which was read from a gif image.

Here are my functions...

public Byte[] GetDocument(string DocumentName)
{
  string strdocPath;
  strdocPath = DocumentName;
  FileStream objfilestream = new 
FileStream(strdocPath,FileMode.Open,FileAccess.Read);
  int len = (int)objfilestream.Length;
  Byte[] documentcontents  = new Byte[len];
  objfilestream.Read(documentcontents,0,len);
  objfilestream.Close();
  return documentcontents;
}


public static string FromUTF8ByteArray(Byte[] characters)
{
  System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  string constructedString = encoding.GetString(characters);
  return (constructedString);
}

public class RawEncoding
{
  public static Byte[] GetBytes(string text)
  {
    Byte[] result = new Byte[text.Length];
    for(int i = 0; i < text.Length; ++i)
    {
      result[i] = (Byte)text[i];
    }
    return result;
  }
}

Byte[] utf8Bytes = 
RawEncoding.GetBytes(FromUTF8ByteArray(GetDocument(FILE_NAME)));
Byte[] finalBytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, 
utf8Bytes);
string fileName = "test.gif";
FileStream fst = new FileStream(file_name, FileMode.Create);
fst.Write(finalBytes, 0, finalBytes.Length);
fst.Flush();
fst.Close();

Thanks