How to write/read linked list to a file in cpp. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to write/read linked list to a file in cpp.

Hi guys. I have seen some file handling videos in cpp. But i am having problem to write/read a Node to a file in cpp. I am new to cpp & oop. 01.I used 2 classes . 1 for node (no functions)& 1 for the list(functions). & i tried to overload the file operators. 02.Where should i write the 'friend function'for overload? (I think in Node class) . If so then how to write? / read? While taking input in "list class" functions like create/display? 03. Thanks in advance.

19th Oct 2019, 4:46 AM
BoltAction Gaming
BoltAction Gaming - avatar
1 Answer
0
If you want to write a node to a file or read one from a file, you have to overload the << and >> operator for the node class. If you want to be able to perform both operations on your list too, you also have to overload the list operators. The overloading works in the same fashion the standard in-/ output stream operators are overloaded. A class can only "give its friendship away", so the declaration belongs into the class which operators should be overloaded. Here is an example code showing how to do writing to a file: https://code.sololearn.com/cILTfNO3OnSC/?ref=app Reading from a file can be implemented likewise, with an object of std::ifstream instead. For further questions, actually seeing your code would be quite helpful.
20th Oct 2019, 12:07 AM
Shadow
Shadow - avatar