Updating record in random access file

Apart from the simplest of applications, most programs have to read or write files.It may be just for reading a config file, or a text parser or something more sophisticated.

updating record in random access file-50updating record in random access file-45updating record in random access file-11

This code sample shows a simple binary file being opened for writing, with a text string (char *) being written into it.

Normally you see this with a text file, but you can write text to a binary file.

Software reading text files have to deal with these other meanings.

Binary files a stream of bytes, and modern languages tend to work with streams rather than files.

It's a little onerous if you are after performance, so you might limit this to debugging.

On Windows, there is little overhead outputting text to the system debugger. The second and third parameters are the size of the characters and the length of the string.

When you open a file, you specify how it is to be opened—whether to create it from new or overwrite it and whether it's text or binary, read or write and if you want to append to it.

This is done using one or more file mode specifiers that are single letters "r", "b", "w", "a" and " " in combination with the other letters.

This example opens a binary file for writing and then writes a char * (string) into it.

The FILE * variable is returned from the fopen() call.

Although you could just check for ft being non-zero (success), this example has a File Success() function to do this explicitly.

Tags: , ,