Reading Structs from a binary file

Hello everyone, I am doing an assignment where I need to implement queues as linked lists. So far the linked lists part is going well but the next part is to add persistence. I was given this function to read the structs into a list from a binary file.

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
  void restoreList(course*& a) 
  { 
    fstream fin; 
    fin.open("courses.dat", ios::binary|ios::in); 
    if (!fin) 
      return;

    // read the number of objects from the disk file 
    int nRecs; 
    fin.read((char*)&nRecs, sizeof(int));

    // read the objects from the disk file 
    for (int i = 0; i < nRecs; i++) 
    { 
      course* t = new course; 
      fin.read((char*)t, sizeof(course)); 
    if (a == nullptr) //I wrote this part of the program to build a linked list
		{
			a = t;
			a->next = nullptr;
		}
		else
		{
			course *p = a;
			course *prev = 0;
			for (p, prev; p; prev = p, p = p->next);
			prev = t;
			prev->next = 0;
		}
    } 
    fin.close(); 
  }


I added a prototype and called the function in this way. And yes I included fstream.


1
2
void restorelist(course *&); //function prototype
restorelist(start); // is how I called the function in the main 


This is the struct I am attempting to read

1
2
3
4
5
6
7
8
struct course
{
	string course_Name;
	string term;
	int units;
	char grade;
	course *next; //link
};


When I added this part in I get an error when building.
Severity Code Description
Error LNK1120 1 unresolved externals
Severity Code Description Project File Line
Error LNK2019 unresolved external symbol "void __cdecl restorelist(struct course * &)" (?restorelist@@YAXAAPAUcourse@@@Z) referenced in function _main


I've attempted to read on these errors but I am afraid I still have not much of a clue as to why this has happened. Likewise I did some reading on binary file I/O and it seems to me that the function I was provided should work but I know very little about reading and writing to binary at this point so I can't say for certain.

Much thanks for the assistance.

Last edited on
closed account (48T7M4Gy)
course *& is a bit of a worry. Shouldn't it just be course * in your function prototype? Besides, where in the function do you use 'start' ?
Last edited on
I'm sorry in the definition it should say a not start. I will edit it to reflect that. I saw that because I was given course *& in this function definition that the prototype should also look similar. In the main course * start = nullptr is my first statement.
Last edited on
closed account (48T7M4Gy)
OK Cheers :)
I've been able to use this code in main but if i try to implement this code as a function I run into issues. I've also tried using course ** instead but I still get the same error.
Line 16: You can't do a binary read of objects that contain std::string data types (course_Name, term). std::string is a complex data type that includes a pointer to memory allocated on the heap. When the course object was written, that internal pointer within the string object pointed to memory in the writer's heap. That pointer is not going to be valid when you read it back in.

Line 26: What's the point of the comma operator in the first term of the for statement?
Last edited on
That makes sense now why this function isn't working. Is there a way to read strings or am I better off just changing my struct to deal with c style strings? Thanks for the help.

Edit: The linked list is supposed to be implemented as a queue I use the for loop to find the end of the linked list so I know where to add the new course.

Second Edit: I think misunderstood what you asked about the for loop. The reason I use the comma is because that's the only way I know how to get the last node(node before the null) in a linked list which is what I believe prev will point to when it stops.
Last edited on
Is there a way to read strings

You need to know the format of how the string is written to the file. Since you haven't posted that, I can't tell you the best way to read it.

I use the for loop to find the end of the linked list

That doesn't answer my question. Do you understand what the comma operator does? p, in your for loop serves no purpose.




You need to know the format of how the string is written to the file. Since you haven't posted that, I can't tell you the best way to read it.


The next part of the assignment is to write a function that writes to a file. So I'm afraid I don't really know how it would be formatted at this point.
I'm afraid I don't really know how it would be formatted at this point.

Is the file "courses.dat" provided to you already containing data?

If so, whoever provide the file should be able to give you a precise description of the layout. If for some reason that information is not available, you might have to open the file in a hex editor to view the contents and try to figure it out.
Is the file "courses.dat" provided to you already containing data?

No this file was just an example I was not provided any files already I need to use my program to read lists, add to this list or create a new one, then I need to be able to write this updated information to a binary file. So it seems like I should try and figure out how to write this struct to a binary file and then I can worry about reading it.
closed account (48T7M4Gy)
I should try and figure out how to write this struct to a binary file and then I can worry about reading it.


That's the right way to go. Basically with persistence you save the object's members individually and then reconstruct an object on retrieving those members from a file. The object is saved member by member not as an object.
Alright thanks for the help everyone I'm going to give it another shot with this new information.
closed account (48T7M4Gy)
That's the way alalakaw, let us know how you get on.
Topic archived. No new replies allowed.