but it can just show one number which is the first number in my .dat file.
for example, in my .dat file, the first four number are 00 00 30 E0, when I use this code to open the file, it just show 0.0000 which is the first number.
So, how can I show all of the number in my .dat file?
#include <stdio.h>
int main()
{
FILE *fp;
float fNUM;
float numArray[10]; // an array to store the numbers (as an example)
if ((fp = fopen("test.dat", "r")) == NULL) {
printf("Cannot open file.\n");
}
else
{
// this example assumes you know that the test.dat file has
// 10 numbers so we create a loop to read in 10 numbers and
// store them in my array of size 10.
for (int i = 0; i < 10; i++) {
fscanf(fp, "%f", &fNUM); // read it
numArray[i] = fNUM;
}
fclose(fp); // close the file.
// now lets check the 10 numbers i had.
for (int i = 0; i < 10; i++)
printf("%f\n", numArray[i]);
}
return 0;
}
as you said
//this example assumes you know that the test.dat file has
// 10 numbers so we create a loop to read in 10 numbers and
// store them in my array of size 10.
My data comes from a CCD camera, the data format is like 00 00 23 E0 (four bytes to one pixel), and I have a 1350*1040 CCD camera.
So how can I read and print these whole data on the screen?
Well the values you show are not floats (as per your original post) and are hex values. Is the values in the data file stored as ascii like 0A which would be two bytes "0" and "A" or is it the actual value 0A hex?
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
FILE *fp;
int fileSize = 0; // for byte count
int fileStartPos; // to store file start pos
int index = 0; // position in allocated memory
unsignedint result;
if ((fp = fopen("test.dat", "r")) == NULL) {
printf("Cannot open file.\n");
}
else
{
// store the start of the file
fileStartPos = ftell(fp);
// determine the size by looping through the file
// and increasing fileSize for each byte i read.
while (fscanf(fp, "%x ", &result) == 1)
++fileSize;
// fileSize now holds number of bytes to read, allocate
// memory to hold our data.
unsignedchar * myData = (unsignedchar *)malloc(fileSize);
// position us back to the start of our file ready
// to read in the bytes as per the size
fseek(fp, fileStartPos, SEEK_SET);
// read in the data
while (fscanf(fp, "%x ", &result) == 1)
myData[index++] = (unsignedchar)result;
// now lets display what we have.
for (index = 0; index < fileSize; index++)
printf("%X ", myData[index]); // change to %d for decimal
// always remember to release the memory once we
// are finished with it.
free(myData);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main()
{
FILE *fp;
unsignedlong fileLen;
unsignedchar *buffer;
// Attempt to open the file as binary
fp = fopen("2.dat", "rb");
if (fp)
{
// grab the file length
fseek(fp, 0, SEEK_END);
fileLen = ftell(fp);
fseek(fp, 0, SEEK_SET);
// Allocate memory for our buffer. filelen holds the
// size of our datafile
buffer = (unsignedchar *)malloc(fileLen + 1);
if (!buffer)
{
printf("Problem allocating memory!");
// we hit a problem, close the file.
fclose(fp);
}
else
{
//Read file contents into our buffer
fread(buffer, fileLen, 1, fp);
// just as a test show the first 15 bytes just so we
// know we have the correct data from the file :)
for (int i = 0; i <= 15; i++)
printf("%x ", buffer[i]);
// close the file
fclose(fp);
// Free the memory we allocated.
free(buffer);
}
}
return 0;
}