Relative Content

Tag Archive for file-handling

“Read” a file without using a file pointer

I was asked this question in an interview. I’m somehow supposed to “read” a file into my C program as input without using a file pointer (including “f” functions, e.g. fgets, fscanf etc.). I’m also not allowed to redirect it using the terminal, i.e. no system or exec calls. The program will not get the file during runtime.

hash with file instead of array

Is it possible to use hash function but with File instead of Array, and it’s gonna be saving the record in a file position and then search will fseek to that position, but I’m not sure how to open a file with let’s say 1000 line, do you think that method could work?

How are Java ByteBuffer’s limit and position variable’s updated?

There are two scenarios: writing and reading
Writing:
Whenever I write something to the ByteBuffer by calling its put(byte[]) method the position variable is incremented as: current position + size of byte[] and limit stays at the max.
If, however, I put the data in a view buffer then I will have to, manually, calculate and update the position
Before I call the write(ByteBuffer) method of the channel to write something, I will have to flip() the Bytebuffer so that
position points to zero and limit points to the last byte that was written to the ByteBuffer.
Reading:
Whenever I call the read(ByteBuffer) method of a channel to read something, the position variable stays at 0 and the limit variable of the ByteBuffer points to the last byte that was read. So, if the ByteBuffer is smaller than the file being read, the limit variable is pushed to max
This means that the ByteBuffer is already flipped and I can proceed to extracting the values from the ByteBuffer.
Please, correct me where I am wrong 🙂

Criteria for selecting timeout value?

Situation: a piece of software reads frames of data from a file in a seperate thread and puts it on a queue, emptied by another thread. That second thread periodically checks on the queue and fails rather gracefully, by showing an error message stating the read timed out, if no data is available within a certain amount of time. Initially this timeout was set to 200mSec. There was no real reasoning behind that constant though, but it worked fine. We measured on a couple of machines and for large data frames, larger than what would be used by customers, a read took like 20mSec whith no other load on the machine.

Quickly compute added and removed lines

I’m trying to compare two text files. I want to compute how many lines were added and removed. Basically what git diff --stat is doing. Bonus points for not having to store the entire file contents in memory.

How to design an application that read files using MVC pattern?

I am going to write an application that reads data from a csv file, and display that data to the user. I intend to apply an MVC design pattern to create this application. A user will interact with this application through a console. Hence the view is a console.