Updating a file random access in c comverse backdating settlement
It'll help you to organise your thoughts, and help us when you ask questions.Read the record, unpack it, perform whatever computations you need for the update, pack the fields back into the record, seek to the start of the record again, and write it back. Faster to code than to say: import struct thefile = open('somebinfile', 'r b') record_size = struct.calcsize(format_string) thefile.seek(record_size * record_number) buffer = thefile.read(record_size) fields = list(struct.unpack(format_string, buffer)) # Perform computations, suitably modifying fields, then: buffer = struct.pack(format_string, *fields) thefile.seek(record_size * record_number) thefile.write(buffer) thefile.close( )This approach works only on files (generally binary ones) defined in terms of records that are all the same, fixed size; it doesn’t work on normal text files.That will give us the number of characters inside the string instead of the array length.
Similarly, tellp () function is used to determine the next position to write a character while writing into a file. For example, given an fstream object a File: So tellg () and tellp () are the two very useful functions while reading from or writing into the files at some certain positions.If we want to perform read or write operations to the disk in character by character fashion, it will be very wasteful of computer time. Suppose ,we want to write a file, say 53 bytes long to the disk .After writing it, the next file will start from 54th byte on the disk.That’s what the part is unnecessary but still recommended for clarity on Unix and Unix-like systems—however, it’s absolutely crucial on other platforms, such as Macintosh and Windows).Here we will discuss how to access files randomly, forward and backward.
Likewise, we intend to move in the backward direction by providing a negative number.