open dat file and print on screen

Can some one help me that plzzzzzzz!!!!!!
Why I cant open the .dat file and print on screen with this code?
Thank you varrrry much

[code]
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
float fNUM;
if(argc!=2) {
printf("You forgot to enter the filename.\n");
exit(1);
}
if((fp=fopen(argv[1], "r"))==NULL) {
printf("Cannot open file.\n");
exit(1);
}


fprintf(fp,"%f",&fNUM);
return 0;
}
Last edited on

Use fscanf ( http://www.cplusplus.com/reference/cstdio/fscanf/ ) such as my example below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <stdio.h>
#include <stdlib.h>

int main()
{
	FILE *fp;
	float fNUM;

	if ((fp = fopen("test.dat", "r")) == NULL) {
		printf("Cannot open file.\n");
	}
	else
	{
		fscanf(fp, "%f", &fNUM);	// read it
		printf("%f", fNUM);			// show it
		fclose(fp);					// you forgot this, always close!
	}

	return 0;
}
thank you very much

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?

Thank you very much~
can some one help me plzzzzzzzzzzzzzzzzzz!!!

My dat files looks like this:

972.000000 552.000000 351.000000 109.000000 5.000000 611.000000 494.000000 453.000000 561.000000 632.000000

My code to read in the 10 numbers looks like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#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;
}
Thank you very much for the code~~~~~~
Your welcome :)
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?


Thhhhhhhank you vvvvvvery much ½½½½½

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?

Anyway...

assuming its ascii, my test.dat looks like this:

0A E1 22 12 A1 32 10 01 12 42

My code to read it, grab its size and allocate memory for it looks like this..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#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
	unsigned int 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.
		unsigned char * myData = (unsigned char *)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++] = (unsigned char)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;
}
This is the data how it looks like

00 00 60 41 00 00 70 41 00 00 40 41 00 00 C0 40 00 00 88 41 00 00 50 41 00 00 00 41 00 00 20 41 00 00 10 41 00 00 00 41 00 00 30 41 00 00 70 41 00 00 50 41 00 00 40 41 00 00 20 41 00 00 80 41 00 00 20 41 00 00 40 41 00 00 E0 40 00 00 80 41 00 00 E0 40 00 00 60 41 00 00 30 41 00 00 30 41 00 00 30 41 00 00 E0 40 00 00 80 41 00 00 E0 40 00 00 20 41 00 00 40 41 00 00 00 41 00 00 30 41 00 00 10 41 00 00 10 41 00 00 20 41 00 00 20 41 00 00 50 41 00 00 50 41 00 00 00 41 00 00 20 41 00 00 50 41 00 00 40 41 00 00 30 41 00 00 50 41 00 00 20 41 00 00 00 41 00 00 30 41 00 00 C0 40 00 00 20 41 00 00 60 41 00 00 10 41 00 00 40 41 00 00 20 41 00 00 30 41 00 00 E0 40 00 00 30 41 00 00 10 41 00 00 00 41 00 00 30 41 00 00 50 41 00 00 50 41 00 00 20 41 00 00 30 41 00 00 10 41 00 00 00 41 00 00 70 41 00 00 10 41 00 00 30 41 00 00 10 41 00 00 88 41 00 00 E0 40 00 00 00 41 00 00 60 41 00 00 30 41 00 00 30 41 00 00 A0 40.................................

every four bytes correspond to one pixel in the picture.


I will try you code firstly~

Thank you very much for your time~
why I cant open the data by your code??? neeeeed helppppppp~~~~~~~

The code I provided works fine using your example of the data file. Provide me with the datafile you are attempting to read.

Well your datafile isn't ASCII but its binary and explains why my example didn't work.

You really should be trying to resolve this yourself otherwise you'll never learn. A modified version of the code is below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

int main()
{
	FILE *fp;
	unsigned long fileLen;
	unsigned char *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 = (unsigned char *)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;
}


Topic archived. No new replies allowed.