Mark
Mark

Reputation: 21

File Handling Text file or Binary file

Which file handling technique is better to apply for making a project of library management system in C++. As in both everything can be done as updating and deleting of the records entered, but which is preferred.

  1. Text files

  2. Binary files

Upvotes: 0

Views: 123

Answers (2)

nitish-d
nitish-d

Reputation: 241

One of the difference that i know that in a text file the text and characters are stored one character per byte, as we expect. But numbers are stored as strings of characters. Thus, 12345, even though it occupies 4 bytes in memory, when transferred to the disk using fprintf(), would occupy 5 bytes, one byte per character. Here if large amount of data is to be stored in a disk file, using text mode may turn out to be insufficient. The solution is to open the file in binary mode and use those functions (fread() and fwrite()) ) which store the numbers in binary format. It means each number would occupy same number of bytes on disks as it occupies in memory.  This will result different size of your file.

Upvotes: 1

holzkohlengrill
holzkohlengrill

Reputation: 1222

I did the same project back in time. I chose a text file format, so I would prefer text files because you can read the data you stored. I think regarding performance there is no difference.

EDIT: OK, apparently there is a slight performance difference as stefaanv mentioned.

Binary format typically uses fewer CPU cycles. However that is relevant only if your application is CPU bound and you intend to do serialization and/or unserialization on an inner loop/bottleneck. Remember: 90% of the CPU time is spent in 10% of the code, which means there won’t be any practical performance benefit unless your “CPU meter” is pegged at 100%, and your serialization and/or unserialization code is consuming a healthy portion of that 100%.

Upvotes: 1

Related Questions