Extract from binary file

Write your question here.
I wrote a program that reads this file and saves all data from the sensor A into the text file A.txt. The rest of the data should be ignored. locTime shall be shown in the format: "Tue Oct 05 15:54:15 2009" and the temperature shall be rounded to 1 decimal place.
Test case
Contents of A.txt
Data A
13.3, Tue Oct 06 15:54:15 2009
2.4, Tue Oct 06 15:54:21 2009
6.6, Tue Oct 06 15:54:22 2009
8.5, Tue Oct 06 15:54:25 2009

But there is something wrong with this code. Who can help me edit it.
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
  //#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <time.h>
using namespace std;

struct DataRecord
{              
	char logger; // Define logging Point A, B, C, …
	double temp; // Logged Temperature
	time_t locTime; // Local Time
};


void main()
{
	FILE *pOpenFile,*pStoreFile;
	DataRecord dataRecord;
	char achDouble[24];

	memset(achDouble, 0, sizeof(achDouble));
	memset(&dataRecord, 0, sizeof(DataRecord));

	char achOpenFileName[] = {"timelog.dat"};//打开的文件名
	char achStoreFileName[] = {"filea.txt"};//存储的文件名

	//控制台打印打开、保存的文件名称
	cout << achOpenFileName <<endl;
	cout << achStoreFileName <<endl;

	pOpenFile = fopen(achOpenFileName, "rb");
	pStoreFile = fopen(achStoreFileName, "w");

	//控制台打印文件的File流是否为空
	cout << pOpenFile <<endl;
	cout << pStoreFile <<endl;

	if (NULL == pOpenFile )
	{
		cout <<"打开文件FILE为空"<<endl;
		return -1;
	}

	if( NULL == pStoreFile )
	{
		cout <<"存储文件FILE为空"<<endl;

		return -1;
	}

	//读取二进制文件 并写入filea.txt
 	while( 0 != fread(&dataRecord, sizeof(DataRecord), 1, pOpenFile ))
 	{
		struct tm *ptm;
		ptm = localtime( &(dataRecord.locTime));

		//将double的数据格式化保留小数点后面一位
		sprintf_s( achDouble, "%.1f", dataRecord.temp);
		
		//在控制台显示读取到的数据
		cout << dataRecord.logger <<" ";
		cout <<achDouble <<" ";
		cout << asctime(ptm) <<endl;
	
		//将数据写入另外一个文件
 		fwrite( &(dataRecord.logger), sizeof(char) , 1, pStoreFile);//写标识
		fwrite( " ",strlen(" "),1,pStoreFile); //这一行的目的是让标示符和后面的温度之间有空格
		fwrite( achDouble, strlen(achDouble) + 1,1, pStoreFile);//写温度
		fwrite( asctime(ptm), strlen(asctime(ptm)) + 1,1, pStoreFile);//写日期

		//清空临时数组
		memset(achDouble, 0,sizeof(achDouble));
 	}
	
	//关闭文件
	if(pOpenFile)
	{
		fclose(pOpenFile);
		pOpenFile = NULL;
	}
	if ( pStoreFile )
	{
		fclose(pStoreFile);
		pStoreFile = NULL;
	}

	getchar();
	return 0;
}
Is there any reason why you use FILE instead of fstream? It would be easier.

This for instance
fwrite( achDouble, strlen(achDouble) + 1,1, pStoreFile);
stores the string including the termination 0 (due to the +1) and of course no , or other separator.

you may consider (if you still want to use fwrite) to prepare the output entirely with sprintf_s()
1
2
char achOpenFileName[] = {"timelog.dat"};//打开的文件名
char achStoreFileName[] = {"filea.txt"};//存储的文件名 

i think you should remove the braces, like:
1
2
char achOpenFileName[] = "timelog.dat";
char achStoreFileName[] = "filea.txt";


CMIIW
Topic archived. No new replies allowed.