How to read and write to a BINARY file?
-
Hello Can anyone tell me how to read and write to a binary file? Any sample code will be of great help. I want to write multiple structures into a binary file and then read it one by one. Thanks in advance
Just CreateFile with binary access, write it, seek to the front, and read it back record by record. If you want random access, then the records must be the same size. If you want variable length records, just precede each binary record with a DWORD with the record size when you write it, then when you read it back, read the DWORD to get the record size, then read the data block, - this also implies that the file must be sequentially accessed both for writing and reading. Dave.
-
Hello Can anyone tell me how to read and write to a binary file? Any sample code will be of great help. I want to write multiple structures into a binary file and then read it one by one. Thanks in advance
#include <stdio.h>
typedef struct _myStruct
{
int x;
int y;
int z;
} myStruct;// use this main() to create the file
int main()
{
FILE *fp;
char *fileName = "output.dat";
char *mode = "wb";
myStruct theArray[10];
int i;
for (i=0; i<10; i++)
{
theArray[i].x = i;
theArray[i].y = 10-i;
theArray[i].z = i * (10-i);
}
fp = fopen(fileName, mode);
fwrite(theArray, sizeof(struct myStruct), 10, fp);
fclose(fp);
}// use this main() to read the file
/*
int main()
{
FILE *fp;
char *fileName = "output.dat";
char *mode = "rb";
myStruct theArray[10]={};
int i;
fp = fopen(fileName, mode);
fread(theArray, sizeof(myStruct), 10, fp);
fclose(fp);
for (i=0; i<10; i++)
{
printf("Item %d---------\n", i+1);
printf("<%d, %d, %d>\n\n", theArray[i].x, theArray[i].y, theArray[i].z);
}
system("pause");
return 0;
}
*/ -
#include <stdio.h>
typedef struct _myStruct
{
int x;
int y;
int z;
} myStruct;// use this main() to create the file
int main()
{
FILE *fp;
char *fileName = "output.dat";
char *mode = "wb";
myStruct theArray[10];
int i;
for (i=0; i<10; i++)
{
theArray[i].x = i;
theArray[i].y = 10-i;
theArray[i].z = i * (10-i);
}
fp = fopen(fileName, mode);
fwrite(theArray, sizeof(struct myStruct), 10, fp);
fclose(fp);
}// use this main() to read the file
/*
int main()
{
FILE *fp;
char *fileName = "output.dat";
char *mode = "rb";
myStruct theArray[10]={};
int i;
fp = fopen(fileName, mode);
fread(theArray, sizeof(myStruct), 10, fp);
fclose(fp);
for (i=0; i<10; i++)
{
printf("Item %d---------\n", i+1);
printf("<%d, %d, %d>\n\n", theArray[i].x, theArray[i].y, theArray[i].z);
}
system("pause");
return 0;
}
*/Thanks for the sample code. I am working on C++ using WIN32 API's in Visual Studio 2003. In the function here for binart read/write, i am using the functions, _write and _ read. Does that make any difference in writing/reading? compared to using fwrite and fread?
-
Thanks for the sample code. I am working on C++ using WIN32 API's in Visual Studio 2003. In the function here for binart read/write, i am using the functions, _write and _ read. Does that make any difference in writing/reading? compared to using fwrite and fread?
-
Thanks for the sample code. I am working on C++ using WIN32 API's in Visual Studio 2003. In the function here for binart read/write, i am using the functions, _write and _ read. Does that make any difference in writing/reading? compared to using fwrite and fread?
dipuks wrote:
i am using the functions, _write and _ read.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Hello Can anyone tell me how to read and write to a binary file? Any sample code will be of great help. I want to write multiple structures into a binary file and then read it one by one. Thanks in advance