Read and write binary
-
Can anyone help me, or at least point me to some good sites that will help with reading and writing binary files on the fly without using an array?
Please check out my articles: The ANZAC's articles
-
Can anyone help me, or at least point me to some good sites that will help with reading and writing binary files on the fly without using an array?
Please check out my articles: The ANZAC's articles
Binary files and arrays have nothing to do with the other.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Binary files and arrays have nothing to do with the other.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
Ok then, well that only answered part of my question. How do i read and write a binary file, what are some keywords i should be looking up?
Please check out my articles: The ANZAC's articles
-
Ok then, well that only answered part of my question. How do i read and write a binary file, what are some keywords i should be looking up?
Please check out my articles: The ANZAC's articles
-
Ok then, well that only answered part of my question. How do i read and write a binary file, what are some keywords i should be looking up?
Please check out my articles: The ANZAC's articles
That all depends on what you are using to open/create the file. Some possibilities include:
// C
fopen()
CreateFile()// MFC
CFile// C++
ofstreamEach of these has their own way of opening/creating a binary file.
"Love people and use things, not love things and use people." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch
-
Ok then, well that only answered part of my question. How do i read and write a binary file, what are some keywords i should be looking up?
Please check out my articles: The ANZAC's articles
With fopen, fread and fwrite. Here's a rough n nasty example:
#include <stdio.h>
#include <stdlib.h>int main()
{
FILE *fp;
long i;
long fLen;
long tmpValue;// try to open an existing file fp = fopen("test.dat", "r+b"); // if failed, then create a file if (fp == NULL) { printf("File doesn't exist\\n"); printf("creating...\\n"); fp = fopen("test.dat", "w+b"); } // if file opened/created ok continue if (fp) printf("File opened successfully.\\n"); // otherwise, nick-off else return -1; // write some binary data to the file for (i=0; i<100; i++) fwrite(&i, sizeof(i), 1, fp); // get length of file fseek(fp, 0, SEEK\_END); fLen = ftell(fp); // return file pointer to beginning of file fseek(fp, 0, SEEK\_SET); // loop through, reading 1 long at a time from the file and printing it's value for (i=0; i<fLen/sizeof(long); i++) { fread(&tmpValue, sizeof(long), 1, fp); printf("%4d", tmpValue); } fclose(fp); return 0;
}
-
With fopen, fread and fwrite. Here's a rough n nasty example:
#include <stdio.h>
#include <stdlib.h>int main()
{
FILE *fp;
long i;
long fLen;
long tmpValue;// try to open an existing file fp = fopen("test.dat", "r+b"); // if failed, then create a file if (fp == NULL) { printf("File doesn't exist\\n"); printf("creating...\\n"); fp = fopen("test.dat", "w+b"); } // if file opened/created ok continue if (fp) printf("File opened successfully.\\n"); // otherwise, nick-off else return -1; // write some binary data to the file for (i=0; i<100; i++) fwrite(&i, sizeof(i), 1, fp); // get length of file fseek(fp, 0, SEEK\_END); fLen = ftell(fp); // return file pointer to beginning of file fseek(fp, 0, SEEK\_SET); // loop through, reading 1 long at a time from the file and printing it's value for (i=0; i<fLen/sizeof(long); i++) { fread(&tmpValue, sizeof(long), 1, fp); printf("%4d", tmpValue); } fclose(fp); return 0;
}
Thanks, this looks quite helpful, i will get to work on this :)
Please check out my articles: The ANZAC's articles