Reading a .dat file

I have a .dat file that contains this:
1
2
3
     @Q@     €J@      O@     €L@     ÀX@     ÀS@     €Q@      O@     ÀQ@      N@      X@     €J@     ÀV@     @X@      M@      R@ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ]   ؐ    Mitcha ÌÌÌAdru ÌÌÌÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ      J@     €W@      R@     €L@     @U@     ÀX@     €V@      Q@      O@     €K@     @S@     €T@     @T@     ÀU@      W@     @U@     ÀR@     €R@&   º6    James ÌÌÌÌHarkley ÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444      ÀR@      P@     €W@     €Q@      L@     ÀP@      O@     €V@     @X@     ÀX@     €V@     €T@     ÀV@     @W@ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ"   âê	    Jake ÌÌÌÌÌChoy ÌÌÌÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ     €T@     €Q@     @X@     @P@     @P@     €T@     @U@     @U@     €L@      U@     €R@      R@     €M@     ÀQ@     €S@      I@      R@      J@   }T    David ÌÌÌÌHansen ÌÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444       T@     €Q@      S@      V@      L@     @T@     @V@      W@     ÀR@     €S@     €P@     @S@      L@     €O@     €M@      W@      T@      O@   |˜    Matt ÌÌÌÌÌFaria ÌÌÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444       L@      V@     @T@     €M@     ÀS@     €V@      X@      P@      N@      S@     ÀX@      X@      N@     ÀX@     ÀT@      I@     €K@ÌÌÌÌÌÌÌÌ   îß    Maria ÌÌÌÌDallan ÌÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 ÌÌÌÌÌÌÌÌ     €N@     @X@      T@     €P@     @T@     ÀV@     €W@      T@     €Q@      R@      M@     €X@     @X@     €T@     €T@     @U@     €W@     @U@[   é	    Brandon ÌÌLay ÌÌÌÌÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444      €V@     €L@      J@     ÀX@     @P@     €U@     ÀV@     @Q@      J@      J@      P@     @P@     ÀR@     ÀS@     €I@ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ   Îù    Bruno ÌÌÌÌCondello ÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ     €J@      L@      M@     €U@     ÀX@     @T@     ÀS@     €V@     €S@     ÀT@      S@      V@     ÀR@     @P@      M@     ÀP@      S@     ÀQ@&   ÇÑ
    Lui ÌÌÌÌÌÌDonghui ÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 JAC444       Q@     €I@     @Q@     @U@     @U@     ÀW@     @U@     ÀQ@     @R@     @R@     ÀS@     @S@      X@     ÀQ@      T@     €N@     €O@ÌÌÌÌÌÌÌÌ#   Ÿ8    Alex ÌÌÌÌÌChow ÌÌÌÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 ÌÌÌÌÌÌÌÌ     ÀV@     ÀR@      Q@     €K@      R@      V@      R@     @R@     @S@      V@     €T@     ÀR@     @X@ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ<   i> 
   Phuong ÌÌÌLy ÌÌÌÌÌÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ     €U@     @W@     ÀS@     €M@     €Q@     @V@     ÀU@     ÀU@     ÀP@     €X@      Q@     €Q@     €W@     ÀV@     €M@     €M@     @W@ÌÌÌÌÌÌÌÌ   `â    Arthur ÌÌÌBradlow ÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 EAC397 ÌÌÌÌÌÌÌÌ     @Q@      W@      S@     ÀU@     ÀX@      O@     €P@     €W@      M@     ÀP@     €N@     @V@      J@      T@     €O@     €R@ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ>   gl    Roya ÌÌÌÌÌSakedad ÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ     @S@     €N@     @V@      J@     €N@     €M@     ÀX@      M@      K@     @S@     €S@      N@     ÀW@     @W@     @U@     @X@ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ&   ʽ    Charles ÌÌBrady ÌÌÌÌAPC100 EAC150 ICA002 IOS110 IPC144 ULI101 DBS201 IBC233 INT222 OOP244 DBS301 INT322 SYS366 BAC344 OOP344 DCN455 ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ 


I want to simply read this into a char (just to see how it works) so from that point i can do the specifics like reading a number into an int. Just wondering how i would start this off. I tried:

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
#include <iostream>
#include <fstream>
#include <string>

#define SIZE_BUF   15

using namespace std;

void main()
{
	char bFileName[] = "OOP344_Assignment_3_2011_01.dat";
	char chr = ' ';

	ifstream  iFile(bFileName, ios::binary);
	iFile.read((char *)(&chr), sizeof(chr));
   /*while (!iFile.eof())
   {
      cout << "Read another record." << endl;
      iFile.read(reinterpret_cast<char *>(save), sizeof(save));
   }*/
	iFile.close();

		cout << chr;
	cout << endl << endl;
	cin.get();
	return;
}


But it doesnt work.
chr is a single character. If you want to read part of the file in a buffer you must use an array.
Yea i had char chr[800]; before and took it out to see if it would work. Im getting a bunch of of weird characters stored in chr[800] when i display it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>

#define SIZE_BUF   15

using namespace std;

void main()
{
	char bFileName[] = "OOP344_Assignment_3_2011_01.dat";
	char chr[800];

	ifstream  iFile(bFileName, ios::binary);
	iFile.read((char *)(&chr), sizeof(chr));
	iFile.close();

		cout << chr << endl;
	cin.get();
	return;
}
Because istream::read doesn't append a \0 character to the buffer.
You can use gcount to get the number of character you read
http://www.cplusplus.com/reference/iostream/istream/gcount/
closed account (zb0S216C)
Try using a wide character( wchar_t ).
I used this method after checking out gcout:

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
#include <iostream>
#include <fstream>
#include <string>

#define SIZE_BUF   15

using namespace std;

void main()
{
	int length;
	char * buffer;

	ifstream is;
	is.open ("OOP344_Assignment_3_2011_01.dat", ios::binary );

	// get length of file:
	is.seekg (0, ios::end);
	length = is.tellg();
	is.seekg (0, ios::beg);
	cout << length;
	// allocate memory:
	buffer = new char [length];

	// read data as a block:
	is.read (buffer,length);
	is.close();

	cout.write (buffer,length);

	//writing

	char bFileName[] = "save.txt";
	int i;

	ofstream  oFile(bFileName, ios::binary);
	// Saving the buffer iBuf in the file
	oFile.write(buffer,length);
	oFile.close();

	delete[] buffer;
	cin.get();
	return;
}


the reading works but how do you convert it to ansii??

the reading works but how do you convert it to ansii??


Depends on what is actually stored in that dat file.
Its a binary file
I guessed that much. My question was rather about what is saved in that binary file.
Thats what im not sure about because my professor made the .dat file. He told the class to read the list of students form the binary file (its a file with student names and courses they are taking), sort it and create an ASCII file with it.
Without the file format, you can't really extract any meaningful data from it. He MUST have told you something like, "First four bytes are an int length, the next length bytes are a string" etc.
He most he gave to determine is this:

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

#include "Course.h"

#define MAX_COURSES      18
#define LEN_FN           10
#define LEN_LN           10

class StudentRecordData
{
protected:
   double Marks[MAX_COURSES];
   unsigned int Age;
   unsigned int ID;
   unsigned int CourseCount;
   char FirstName[LEN_FN];
   char LastName[LEN_FN];
   Course  Courses[MAX_COURSES];
};

#endif 


and

1
2
3
4
5
6
7
8
9
10
11
12
#ifndef COURSE_H 
#define COURSE_H

#define LEN_COURSE_NAME  6

struct Course
{
   unsigned char Semester;
   char  Name[LEN_COURSE_NAME+1];
};

#endif 
Last edited on
That's actually exactly what you need to read the file since now you know the structure it was written in. It's been a while since I have dealt with bin files so I don't remember the exact syntax, but the data in the file will follow the structure of the class exactly.

So a read of ((char *)x, sizeof(double * MAX_COURSES)) will give you the Marks field that was written to the file.

Can't remember exactly but you might even be able to read the whole class into a StudentRecordData object with a single instruction.
That's actually exactly what you need to read the file since now you know the structure it was written in.


No you don't. There is quite a number of ways to serialize this. Though unless your professor is an... a not very nice person I mean to say, he will probably have just casted everything to char* and written it to the file in the order he declared the members. Though that means you're f*ed if your system has another endianess or a different size for any of the types than the one your professor is using.


Can't remember exactly but you might even be able to read the whole class into a StudentRecordData object with a single instruction.

Not really, unless you already have a deserialization function available.
Last edited on
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
#include <iostream>
#include <fstream>
#include <string>

#include "StudentRecordData.h"

using namespace std;

StudentRecordData::StudentRecordData()
{
	for(int i=0; i<18; i++)
		Marks[i] = 0;

	ifstream is;
	is.open ("OOP344_Assignment_3_2011_01.dat", ios::binary );
	is.read(reinterpret_cast<char *>(Marks), sizeof(double)*18);

	for(int i=0; i<18; i++)
	{
		if (Marks[i] > 0)
			cout << Marks[i] << " ";
		else
			Marks[i] = 0;
	}

	is.read(reinterpret_cast<char *>(Age), sizeof(int));

	is.close();
}


The read for Marks works but the read for Age gives me a:
Unhandled exception at 0x53afcac8 (msvcr100d.dll) in assignment3.exe: 0xC0000005: Access violation writing location 0xcccccccc.


Why is this happening now?
is.read(reinterpret_cast<char *>(Marks), sizeof(double)*18); and is.read(reinterpret_cast<char *>(Age), sizeof(int)); are things you should never do.

If Age is an integer, reinterpret_cast<char *>(Age) will make it try to read to the location with the same address as the value of Age. Eg: if Age=0, it will try to read at location 0
How would i fix that and my prof showed me this way is there another way to read?
Wait i think i figured it out. I used &Age instead.
Topic archived. No new replies allowed.